Map items with an async mapper and a concurrency limit
A photo app uploads an album by calling the API once per photo. Firing 400 requests at once gets the client rate limited, but uploading one at a time is far too slow. The fix is a worker pool: keep a fixed number of requests in flight and start the next one the moment any of them finishes.
Implement mapWithLimit(items, mapper, limit). It calls mapper for every item, with at most limit calls pending at the same time, and returns a promise for the array of results.
mapWithLimit(items, mapper, limit?) // => Promise<results[]>
// mapper(item, index) => value | Promise<value>
results[i] is the result for items[i], no matter which call finishes first.mapper(item, index). It may return a plain value or a promise.0, 1, 2, ….mapper is called until its promise settles. Never have more than limit pending calls. Start the first limit items right away, then start the next item as soon as any pending call settles. Do not wait for a whole batch to finish.limit is omitted, it is Infinity and every item starts right away.limit is not a positive integer or Infinity, the returned promise rejects with a RangeError and mapper is never called.mapper calls start. Calls that are already pending are left to finish, but their results are ignored.[] without calling mapper.items.const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const uploadTimes = { 'beach.jpg': 300, 'dog.jpg': 100, 'cake.jpg': 100, 'sky.jpg': 100 };
const urls = await mapWithLimit(
Object.keys(uploadTimes),
async (name, index) => {
await sleep(uploadTimes[name]);
return `https://cdn.example.com/${index}/${name}`;
},
2,
);
// urls => [
// 'https://cdn.example.com/0/beach.jpg',
// 'https://cdn.example.com/1/dog.jpg',
// 'https://cdn.example.com/2/cake.jpg',
// 'https://cdn.example.com/3/sky.jpg',
// ]
// Timeline with limit 2:
// t=0 start beach, dog
// t=100 dog done -> start cake (beach is still uploading)
// t=200 cake done -> start sky
// t=300 beach and sky done -> resolve
// Fixed batches of 2 would take 400ms instead.
Tests use mappers that return promises the test settles by hand, then check which items have started. They wait for pending promise callbacks to run before checking, so it does not matter whether your first calls happen synchronously or in a microtask.