我有两个 promise ,一个可能会很慢,另一个会更快。我知道 Promise.all
要么全有,要么全无,因此当慢速超时时,它会导致另一个也无法显示结果。无论如何,我是否可以同时运行两个 Promise,并且如果慢速的 Promise 失败,我仍然可以从快速的 Promise 中获取结果,而无需进行大量回调?
我用谷歌搜索,似乎找不到
var pList = [slowQuery({url: url}), fastQuery({url: url})];
Promise.all(pList).then(function doneFunc(results) {
output.slow = results[0];
output.fast = results[1];
window.respond(output);
}).catch(function errFunc() {
output.slow = [];
output.fast = [];
window.respond(output);
});
}
最佳答案
这可能有用:
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.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race
关于javascript - 如何在nodejs中同时运行两个promise并一一失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35921320/