How to use the all function from es6-promise

Find comprehensive JavaScript es6-promise.all code examples handpicked from public code repositorys.

es6-promise.all is a method that takes an array of promises and returns a new promise that resolves to an array of resolved values of those promises in the same order.

130
131
132
133
134
135
136
137
138
139
140
    */
    waitFor: function(/*array*/ promiseIndexes, /*function*/ callback) {
        var selectedPromises = promiseIndexes.map(function(index) {
            return this._promises[index];
        }, this);
        return Promise.all(selectedPromises).then(callback);
    }


};

fork icon0
star icon4
watch icon2

+ 13 other calls in file

How does es6-promise.all work?

es6-promise.all is a method that takes an array of promises as input and returns a new promise that resolves when all the input promises have resolved or rejects if any of the input promises reject.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// create two promises that resolve with a delay
const promise1 = new Promise((resolve, reject) => {
  setTimeout(() => resolve("Resolved Promise 1"), 2000);
});

const promise2 = new Promise((resolve, reject) => {
  setTimeout(() => resolve("Resolved Promise 2"), 3000);
});

// use Promise.all to handle both promises
Promise.all([promise1, promise2]).then((values) => {
  console.log(values);
  // output: ["Resolved Promise 1", "Resolved Promise 2"]
});

In this example, Promise.all takes an array of promises as input, and returns a new promise that resolves with an array of values from all the input promises, in the order they were provided. The then method is then used to log the resolved values of both promises.