是否有内置函数在q中配置竞赛?

时间:2015-09-29 09:58:58

标签: javascript q

是否有内置函数在q JavaScript库中配置竞赛?

示例:

function fnThatReturnsAPromise1() { /*...*/ }
function fnThatReturnsAPromise2() { /*...*/ }

//Pseudocode: I want to have the first one of these functions that resolves, be the result
q.race([fnThatReturnsAPromise1,fnThatReturnsAPromise2])
.then(function(resultOfWinner) {
  // ...
});

编辑:源中似乎有一种竞争方法,但文档中未提及。 https://github.com/kriskowal/q/blob/v1/q.js#L738

1 个答案:

答案 0 :(得分:2)

v0.9.7以来,Q.race方法完全符合您的要求。但still not documented yet中只有API reference

此外,由于v1.0.1该功能在符合ES6的Q.Promise对象上以.race的形式提供。