Run Async Work Exclusively with a Typed Mutex

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write a class Mutex with runExclusive(fn): run fn (sync or async) only once every task passed in before it has finished, one at a time in the order they were passed, and resolve or reject with fn's own result. A task that fails mustn't block the ones after it. Type runExclusive so it resolves to fn's result type, unwrapping a promise, and rejects anything that isn't a function.

Your code must pass

  • Runs tasks one at a time, in order
  • Resolves with each task's result
  • Keeps going after a task fails
  • Has no type errors
  • Resolves with the task's type
  • Rejects a task that isn't a function