Example : Promise Race 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);
});

// Race returns a value as soon as the first promise is resolved, than executes the .then() method

Promise.race([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
Last Updated: 8/11/2019, 10:51:29 PM