sindresorhus / p-limit

Run multiple promise-returning & async functions with limited concurrency
MIT License
1.97k stars 102 forks source link

Why we need `await Promise.resolve()` in enqueue #82

Closed zingxy closed 3 months ago

zingxy commented 3 months ago
(async () => {
    // This function needs to wait until the next microtask before comparing
    // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
    // when the run function is dequeued and called. The comparison in the if-statement
    // needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
    await Promise.resolve(); 

    if (activeCount < concurrency && queue.size > 0) {
        queue.dequeue()();
    }
})();

Can you give me some concrete example? Thank you!

sindresorhus commented 3 months ago

https://github.com/sindresorhus/p-limit/pull/28