P-Limit Concurrency Control for Task Queues
Cap parallel task execution with p-limit so large batches of promises run at a fixed concurrency instead of exhausting memory or API quotas.
Code
import pLimit from 'p-limit';
const limit = pLimit(4);
async function syncAll(ids) {
const tasks = ids.map((id) =>
limit(() => fetch(`https://api.example.com/items/${id}`).then((r) => r.json())),
);
const results = await Promise.all(tasks);
return results;
}
const started = [];
const tracker = pLimit(2);
const jobs = [1, 2, 3, 4].map((n) =>
tracker(async () => {
started.push(n);
console.log('running', n, 'active:', started.length);
await new Promise((r) => setTimeout(r, 100));
started.pop();
}),
);
await Promise.all(jobs);Report a problem with this exampleOpens a pre-filled issue on GitHub.