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
Old topic but here's my entry; it's essentially @loganfsmyth's solution, but with a few more checks to conform to conventions established by Promise.all():
Promise.any = a => {
return !a.length ?
Promise.resolve() :
Promise.all(a.map(
e => (typeof e.then !== 'function') ?
Promise.reject(e) :
e.then(
result => Promise.reject(result),
failure => Promise.resolve(failure)
)
)).then(
allRejected => Promise.reject(allRejected),
firstResolved => Promise.resolve(firstResolved)
);
};
// Testing...
function delayed(timeout, result, rejected) {
return new Promise((resolve, reject) => {
setTimeout(
() => rejected ? reject(result) : resolve(result),
timeout);
});
}
Promise.any([
delayed(800, 'a'),
delayed(500, 'b'),
delayed(250, 'c', true)
]).then(e => {
console.log('First resolved (expecting b):', e);
});
Promise.any([
delayed(800, 'a', true),
delayed(500, 'b', true),
delayed(250, 'c', true)
]).then(null, e => {
console.log('All rejected (expecting array of failures):', e);
});
Promise.any([
delayed(800, 'a'),
delayed(500, 'b'),
delayed(250, 'c', true),
'd',
'e'
]).then(e => {
console.log('First non-promise (expecting d):', e);
});
// Because this is the only case to resolve synchronously,
// its output should appear before the others
Promise.any([]).then(e => {
console.log('Empty input (expecting undefined):', e);
});