Process Items One at a Time with a Typed Worker

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

The problem

  • medium
  • Code

Write createQueue(worker), where worker is an async function for one item. It returns push(item), which adds the item and returns a promise of that item's result, and onIdle(), a promise that resolves once nothing is waiting or running. Items go through worker one at a time, in the order pushed; one that fails rejects its own promise and the queue carries on. Type it so push only takes the worker's item type and resolves to its result type.

Your code must pass

  • Runs one item at a time, in order
  • Resolves each push with its item's result
  • Carries on after a failure
  • onIdle waits for every item
  • Has no type errors
  • push takes the worker's item type
  • push resolves with the worker's result type