I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Registrati per tradurlo tu.
The Promise.race(iterable)
method returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.
Syntax
Promise.race(iterable);
Parameters
Return value
A Promise
that resolves or rejects as soon as one of the promises in the given iterable resolves or rejects.
Description
The race
function returns a Promise
that is settled the same way as the first passed promise to settle. It resolves or rejects, whichever happens first.
Examples
Using Promise.race
– examples with setTimeout
var p1 = new Promise(function(resolve, reject) { setTimeout(resolve, 500, "one"); }); var p2 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, "two"); }); Promise.race([p1, p2]).then(function(value) { console.log(value); // "two" // Both resolve, but p2 is faster }); var p3 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, "three"); }); var p4 = new Promise(function(resolve, reject) { setTimeout(reject, 500, "four"); }); Promise.race([p3, p4]).then(function(value) { console.log(value); // "three" // p3 is faster, so it resolves }, function(reason) { // Not called }); var p5 = new Promise(function(resolve, reject) { setTimeout(resolve, 500, "five"); }); var p6 = new Promise(function(resolve, reject) { setTimeout(reject, 100, "six"); }); Promise.race([p5, p6]).then(function(value) { // Not called }, function(reason) { console.log(reason); // "six" // p6 is faster, so it rejects });
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Promise.race' in that specification. |
Standard | Initial definition in an ECMA standard. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Promise.race' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Servo |
---|---|---|---|---|---|---|---|
Basic Support | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Feature | Android | Chrome for Android | Edge Mobile | Firefox for Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic Support | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |