Skip to curriculum
Jump to the editor

14.7. Map with a concurrency limit

Medium
Topics
20–30 min

Promise.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

JAVASCRIPT
function mapLimit(items, limit, fn) // returns a Promise of an array
  • items: an array.
  • limit: the most calls of fn running at once: a whole number of 1 or more, or Infinity for 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] being fn's result for items[i].

Requirements

  1. limit calls start at once (or every item, if there are fewer), in the order of items, unless one of them fails while starting (rule 7): then the ones after it do not start.
  2. There are never more than limit calls running.
  3. As soon as a call finishes, the next item starts in its place: no waiting for a whole group to finish.
  4. The results follow the order of items, whatever order the calls finish in. The input array is not changed.
  5. An empty array resolves with [] without calling fn.
  6. The first failure rejects the promise with its error, and no new item starts after it. (Calls already running finish on their own.)
  7. An error fn throws 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. mapLimit itself never throws.
  8. A limit that is not a whole number of 1 or more (0, negative, 1.5, NaN, missing) rejects with a RangeError before calling fn.

Examples

JAVASCRIPT
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
JAVASCRIPT
// 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 200
JAVASCRIPT
await mapLimit([], 3, fn);        // [] (fn never runs)
await mapLimit([1, 2], 0, fn);    // rejects with RangeError

Environment

  • Plain JavaScript in a browser page; the tests call the global mapLimit from map-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.all may help at the end, but it cannot limit how many calls start: the limit is yours to write.

Hints

  1. Instead of a loop that waits, what if each call, when it finishes, starts the next item itself?
  2. 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
Back to the question
Loading the editor…