1

I have two promises, one is likely to be very slow and the other one is going to be faster. I understand that Promise.all is all or nothing so when the slow one timeouts it causes the other one to not show result as well. Is there anyway I can run two Promises in the same time and if the slow one fails I can still get the result from the fast one without doing a lot of callbacks?

I googled and I can't seem to find an example of

  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);
  });
}
toy
  • 11,711
  • 24
  • 93
  • 176
  • In that callback you've shown you'd still have the fast and the slow result at the same time? – Bergi Mar 10 '16 at 15:59

1 Answers1

3

This might be useful:

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

Arda Keskiner
  • 772
  • 7
  • 23