"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > ## Promise.all: Is it Parallel or Sequential Execution in Node.js?

## Promise.all: Is it Parallel or Sequential Execution in Node.js?

Published on 2024-11-09
Browse:326

## Promise.all: Is it Parallel or Sequential Execution in Node.js?

Promise.all: Parallel or Sequential Execution in Node.js?

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.

Latest tutorial More>

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