Map with a Concurrency Limit, Typed and in Order

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

The problem

  • medium
  • Code
  • Pro

Write mapLimit(items, limit, fn): call the async function fn(item, index) for every item, with at most limit calls running at once, and resolve to their results in the items' order, not the order they finish (reject if any call rejects). Type it generically, so fn is given the items' own type and the result is an array of fn's resolved type, and fn for a different item type is rejected.

Your code must pass

  • Keeps the results in the items' order
  • Never runs more than limit at once
  • Passes each item's index
  • Has no type errors
  • Results have fn's result type
  • fn gets the items' type
  • Rejects fn for another item type