I\'m puzzled by something in the ES6 Promise API. I can see a clear use case for submitting multiple async jobs concurrently, and \"resolving\" on the first success. This wo
Is there something in the API that permits a "raceToSuccess" kind of behavior
Soon, there almost certainly will be. There is a Stage 3 proposal for Promise.any:
Promise.any()takes an iterable of Promise objects and, as soon as one of the promises in the iterable fulfills, returns a single promise that resolves with the value from that promise.
So, the following syntax will be valid:
// assume getApi returns a Promise
const promises = [
getApi('url1'),
getApi('url2'),
getApi('url3'),
getApi('url4'),
];
Promise.any(promises)
.then((result) => {
// result will contain the resolve value of the first Promise to resolve
})
.catch((err) => {
// Every Promise rejected
});
Promise.any has been implemented in Spidermonkey, and there are some polyfills available.