Promise.race Function (Promise)

Creates a new promise that will resolve or reject with the same result value as the first promise to resolve or reject among the passed in arguments.

Syntax

Promise.race(iterable)  

Parameters

iterable
Required. One or more promises.

Remarks

If one of the promises in iterable is already in a resolved or rejected state, Promise.race returns a promise resolved or rejected in the same way with the result value equal to the value used to resolve (or reject) that promise. If multiple promises in iterable are already resolved or rejected, Promise.race returns a promise resolved in the same way as the first promise iterated. If no promise in iterable resolves or rejects, the promise returned from Promise.race also does not resolve or reject.

Example

var p1 = new Promise(function(resolve, reject) {  
    setTimeout(resolve, 0, 'success');  
});  
var p2 = new Promise(function(resolve, reject) { });  
var p2 = new Promise(function(resolve, reject) { });  

var race = Promise.race( [p1, p2, p3] );  
race.then(function(result) {  
    console.log(result);  
});  

// Output:  
// success  

var race = Promise.race( [Promise.reject('failure'),  
    Promise.resolve('success')] );  
race.catch(function(result) {  
    console.log(result);  
});  

// Output:  
// failure  

Requirements

Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.

See Also

Promise Object