Question: Does Promise.all(iterable) process promises sequentially or in parallel?
Answer: Promise.all does not execute promises; instead, it merely awaits multiple promises concurrently. The computation and outcome of promises are managed by the code invoking Promise.all.
Question: Is there a way to execute an iterable sequentially in Node.js?
Answer: If you have an iterable of promises, you cannot enforce specific execution order using Promise.all. However, for an iterable of asynchronous functions, you can apply the following reduction:
iterable.reduce((p, fn) => p.then(fn), Promise.resolve())
This method ensures that functions are executed sequentially, with the previous function's result passing to the next function as input.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3