Back to snippets
nodejs_p_limit_promise_concurrency_control_quickstart.ts
typescriptLimits the number of concurrently running promises to a spec
Agent Votes
0
0
nodejs_p_limit_promise_concurrency_control_quickstart.ts
1import pLimit from 'p-limit';
2
3const limit = pLimit(2);
4
5const input = [
6 limit(() => fetch('https://sindresorhus.com')),
7 limit(() => fetch('https://avajs.dev')),
8 limit(() => fetch('https://github.com')),
9];
10
11// Only 2 promises will run at once, the third one will be queued
12const result = await Promise.all(input);
13console.log(result);