Skip to content

Latest commit

 

History

History
38 lines (25 loc) · 2.28 KB

73-Promises-In-Parallel.md

File metadata and controls

38 lines (25 loc) · 2.28 KB

Running promises in Parallel in javascript

Running promises in parallel in JavaScript refers to executing multiple asynchronous operations simultaneously, without waiting for one operation to complete before starting the next one. This can improve performance and reduce overall execution time. One way to achieve this is by using Promise.all() method which takes an array of promises and returns a new promise that resolves when all the input promises have resolved or rejects when any of the input promises reject.

Running promises in parallel means executing multiple asynchronous operations at the same time, without waiting for any of them to complete before starting the next one. This can greatly improve performance in situations where there are many independent and time-consuming tasks to be performed.

In JavaScript, you can run promises in parallel using Promise.all() method. This method takes an array of Promises as its argument, and returns a new Promise that resolves with an array of all the resolved values or rejects with the reason of the first rejected promise.

Here is an example of running promises in parallel:

const p1 = new Promise((resolve, reject) => {
  setTimeout(() => resolve("Promise 1 resolved"), 500);
});

const p2 = new Promise((resolve, reject) => {
  setTimeout(() => resolve("Promise 2 resolved"), 1000);
});

const p3 = new Promise((resolve, reject) => {
  setTimeout(() => resolve("Promise 3 resolved"), 1500);
});

Promise.all([p1, p2, p3])
  .then(results => console.log(results))
  .catch(error => console.log(error));

In this example, we create three promises (p1, p2, p3) that resolve after different amounts of time using the setTimeout() function. We then pass these promises to Promise.all() method, which returns a new Promise that resolves with an array of all the resolved values. We log the results to the console using .then() method.

Since p1 resolves after 500ms, p2 after 1000ms, and p3 after 1500ms, we would expect to see the following output after 1500ms:

["Promise 1 resolved", "Promise 2 resolved", "Promise 3 resolved"]

Note that if any of the promises passed to Promise.all() method reject, the whole sequence will be rejected and .catch() method will be called with the reason of the first rejected promise.