14.7. Map with a concurrency limit
Topics
This question is in
14. Timeouts, retries and concurrencyPromise.all(items.map(upload)) starts every upload at once. With 200 photos that means 200 requests fighting for the same connection, a browser that queues most of them anyway, and a server that may refuse the burst. Running them strictly one at a time is safe but slow. The middle way is a concurrency limit: keep, say, 4 running, and start the next one the moment any of them finishes.
Write mapLimit(items, limit, fn) in map-limit.js. It calls fn for every item, never with more than limit calls running at the same time, and resolves with the results in the order of items.
Signature
function mapLimit(items, limit, fn) // returns a Promise of an arrayitems: an array.limit: the most calls offnrunning at once: a whole number of 1 or more, orInfinityfor no limit.fn(item, index): the work for one item. It may return a promise or a value, or throw.- Returns a promise of the results,
results[i]beingfn's result foritems[i].
Requirements
limitcalls start at once (or every item, if there are fewer), in the order ofitems, unless one of them fails while starting (rule 7): then the ones after it do not start.- There are never more than
limitcalls running. - As soon as a call finishes, the next item starts in its place: no waiting for a whole group to finish.
- The results follow the order of
items, whatever order the calls finish in. The input array is not changed. - An empty array resolves with
[]without callingfn. - The first failure rejects the promise with its error, and no new item starts after it. (Calls already running finish on their own.)
- An error
fnthrows counts as a failure at the moment it is thrown, so rule 6 applies at once: no item starts after it, not even the rest of the first group.mapLimititself never throws. - A
limitthat is not a whole number of 1 or more (0, negative, 1.5,NaN, missing) rejects with aRangeErrorbefore callingfn.
Examples
const sizes = await mapLimit(["a.jpg", "b.jpg", "c.jpg"], 2, (name) => upload(name));
// a and b start at once; c starts when either is done
// sizes is in the order a, b, c// fn takes 100, 300, 100 and 100 ms for items 1 to 4, limit 2:
// starts: item 1 at 0, item 2 at 0, item 3 at 100, item 4 at 200await mapLimit([], 3, fn); // [] (fn never runs)
await mapLimit([1, 2], 0, fn); // rejects with RangeErrorEnvironment
- Plain JavaScript in a browser page; the tests call the global
mapLimitfrommap-limit.js. - The tests run on a virtual clock that replaces
setTimeout; their tasks take a set number of ms and record how many run at once. Promise.allmay help at the end, but it cannot limit how many calls start: the limit is yours to write.
Hints
- Instead of a loop that waits, what if each call, when it finishes, starts the next item itself?
- How do you put a result in the right place if you do not know when it will arrive?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it