Home >Web Front-end >JS Tutorial >## Does Promise.all Process Promises Sequentially or in Parallel?
Sequential vs. Parallel Processing in Node.js Promise.all
When using Promise.all with an iterable of promises, a common question arises: does it process promises sequentially or in parallel?
Q1: Sequential or Parallel Processing?
Despite its ambiguous documentation, Promise.all does not process promises sequentially or in parallel. Instead, it merely awaits the resolution (or rejection) of all provided promises concurrently.
This means that all promises passed to Promise.all are executed simultaneously as soon as they are created. The final result returned by Promise.all is an array of resolved values or a single rejection value if any of the promises fail.
Q2: Sequential Processing with Promises
Since Promise.all does not enforce sequential processing, if you need to process promises sequentially, you can create a chain of chained promises:
<code class="js">p1.then(p2).then(p3).then(p4).then(p5)....</code>
This approach ensures that each promise is executed after the previous one has resolved.
Alternatively, you can utilize Array::reduce to achieve sequential execution with aniterable of asynchronous functions:
<code class="js">iterable.reduce((p, fn) => p.then(fn), Promise.resolve())</code>
The above is the detailed content of ## Does Promise.all Process Promises Sequentially or in Parallel?. For more information, please follow other related articles on the PHP Chinese website!