Run multiple promise-returning & async functions with limited concurrency using native ES6/ES7
MIT License
⭐️ Enhancement
🐛 Fixes
Published by rxaviers over 2 years ago
🌟 New API (breaking change)
Support for await...of usage by returning an async iterator.
const timeout = ms => new Promise(resolve => setTimeout(() => resolve(ms), ms));
for await (const ms of asyncPool(2, [1000, 5000, 3000, 2000], timeout)) {
console.log(ms);
}
// Call iterator timeout(1000)
// Call iterator timeout(5000)
// Concurrency limit of 2 reached, wait for the quicker one to complete...
// 1000 finishes
// for await...of outputs "1000"
// Call iterator timeout(3000)
// Concurrency limit of 2 reached, wait for the quicker one to complete...
// 3000 finishes
// for await...of outputs "3000"
// Call iterator timeout(2000)
// Itaration is complete, wait until running ones complete...
// 5000 finishes
// for await...of outputs "5000"
// 2000 finishes
// for await...of outputs "2000"
The main difference: 1.x API waits until all of the promises completes, then all results are returned. The new API (thanks to async iteration) let each result be returned as soon as it completes.
The environment baseline of the new 2.x is ES9 (Node v10). The environment baseline of 1.x is ES6.
Published by rxaviers over 2 years ago
🎉 New Features
🐛 Fixes