JavaScript - Promises - Using Promise.race(iteratable)

javascript-promises

// JavaScript - Promises - Using Promise.race(iteratable):

Promise.race is an interesting function.  Instead of waiting for all promises to 
be resolved or rejected, Promise.race triggers as soon as any promise in the 
iteratable is resolved or rejected:

var req1 = new Promise(function(resolve, reject) { 
    // A mock async action using setTimeout
    setTimeout(function() { resolve('First!'); }, 8000);
});
var req2 = new Promise(function(resolve, reject) { 
    // A mock async action using setTimeout
    setTimeout(function() { resolve('Second!'); }, 3000);
});
Promise.race([req1, req2]).then(function(one) {
    console.log('Then: ', one);
}).catch(function(one, two) {
    console.log('Catch: ', one);
});

// From the console:
// Then: Second!
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License