Example : Promise All method

//Promises in ES6 - 'All' and 'Race'

let promiseOne = new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('PromiseOne Resolved!');
  }, 1000);
});

let promiseTwo = new Promise(function(resolve, reject) {
  setTimeout(function() {
    reject('promiseTwo Rejected!');
  }, 2000);
});

// **All expects to get an array of promises, and combines them into one single promise**
// All will wait for all of the promises to finish, and only if ALL of them are resolved will any of them be resolved
// As soon as one single promise is rejected, no resolved values will be passed


Promise.all([promiseOne, promiseTwo])
  .then(function(success) {
  console.log(success);
})
  .catch(function(error) {
  console.log(error);
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Last Updated: 8/11/2019, 10:51:29 PM