javascript promise all 1

javascript promise all

const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise(function(resolve, reject) {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then(function(values) {
  console.log(values);
});
// expected output: Array [3, 42, "foo"]

Here is what the above code is Doing:
1. We create three promises:
– promise1 is already resolved with a value of 3
– promise2 is already resolved with a value of 42
– promise3 will resolve with a value of “foo” in 100ms
2. We pass these promises as an iterable to Promise.all
3. Promise.all waits for all of the promises in the iterable argument to resolve, and then it returns a single Promise
– This returned promise fulfills with an array containing the fulfillment value of each promise in the iterable
– If any of the promises in the iterable are rejected, the returned promise is rejected with the reason of the first promise that rejected

Similar Posts