Promise.race

Promise.race

Promise.race(iterable)方法返回一个 promise,并伴随着 promise对象解决的返回值或拒绝的错误原因, 只要 iterable 中有一个 promise 对象"解决(resolve)"或"拒绝​​​​​​​(reject)"。

语法

Promise.race(iterable

参数

iterable可迭代对象,例如: 一个Array。详情可见 iterable。

返回值

一个待定的 Promise只要给定的迭代中的一个promise解决或拒绝,就采用第一个promise的值作为它的值,从而异步地解析或拒绝(一旦堆栈为空)。

描述

race 函数返回一个 Promise,它将与第一个传递的 promise 相同的完成方式被完成。它可以是完成( resolves),也可以是失败(rejects),这要取决于第一个完成的方式是两个中的哪个。

如果传的迭代是空的,则返回的 promise 将永远等待。

如果迭代包含一个或多个非承诺值和/或已解决/拒绝的承诺,则Promise.race将解析为迭代中找到的第一个值。

示例

Promise.race的异步性

下面的这个例子演示了Promise.race的异步性:

// we are passing as argument an array of promises that are already resolved, // to trigger Promise.race as soon as possible var resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)]; var p = Promise.race(resolvedPromisesArray // immediately logging the value of p console.log(p // using setTimeout we can execute code after the stack is empty setTimeout(function(){ console.log('the stack is now empty' console.log(p } // logs, in order: // Promise { <state>: "pending" } // the stack is now empty // Promise { <state>: "fulfilled", <value>: 33 }

一个空的迭代器会导致返回的promise永远等待:

var foreverPendingPromise = Promise.race([] console.log(foreverPendingPromise setTimeout(function(){ console.log('the stack is now empty' console.log(foreverPendingPromise } // logs, in order: // Promise { <state>: "pending" } // the stack is now empty // Promise { <state>: "pending" }

如果迭代包含一个或多个非承诺值和/已解决/reject的promise,Promise.race则将解析为在数组中找到的第一个值:

var foreverPendingPromise = Promise.race([] var alreadyResolvedProm = Promise.resolve(666 var arr = [foreverPendingPromise, alreadyResolvedProm, "non-Promise value"]; var arr2 = [foreverPendingPromise, "non-Promise value", Promise.resolve(666)]; var p = Promise.race(arr var p2 = Promise.race(arr2 console.log(p console.log(p2 setTimeout(function(){     console.log('the stack is now empty'     console.log(p console.log(p2 } // logs, in order: // Promise { <state>: "pending" }  // Promise { <state>: "pending" }  // the stack is now empty // Promise { <state>: "fulfilled", <value>: 666 } // Promise { <state>: "fulfilled", <value>: "non-Promise value" }

使用Promise.race –  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 }

规范

SpecificationStatusComment
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Promise.race' in that specification.StandardInitial definition in an ECMA standard.
ECMAScript Latest Draft (ECMA-262)The definition of 'Promise.race' in that specification.Living Standard

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support32.0(Yes)29.0No197.1

FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support4.4.432.0(Yes)29No(Yes)8.0