About Parallel Promises

In JavaScript, you can run multiple promises in parallel and then collect the results together:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
let promise1 = new Promise(function(resolve, reject) {
    // get the data…
    resolve(data);
});

let promise2 = new Promise(function(resolve, reject) {
    // get the data…
    resolve(data);
});

let promise3 = new Promise(function(resolve, reject) {
    // get the data…
    resolve(data);
});

Promise.all([promise1, promise2, promise3]).then(
    ([data1, data2, data3]) => {
        // do something with data from all three promises…
    }
).catch(err => {
    console.error(err);
});

For clarification, there are no threads in JavaScript. That is, if you use normal conditions and loops to get the data for your promises, they will be executed consecutively.

But if you use timeouts, Ajax calls, or web workers in the promises, they will act like asynchronous threads.

Tips and Tricks Programming JavaScript