如何在 nodejs 中同时 运行 两个 promise 并一个一个地失败

How to run two promises in the same time and fail one by one in nodejs

我有两个承诺,一个可能会很慢,另一个会更快。我知道 Promise.all 是全有或全无,所以当慢速超时时,它会导致另一个也不显示结果。无论如何,我可以同时 运行 两个 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