Skip to content

Instantly share code, notes, and snippets.

@nikita-graf
Created April 11, 2017 16:39
Show Gist options
  • Save nikita-graf/a9d8efd1a78ed03d677e6b3eb4d0924d to your computer and use it in GitHub Desktop.
Save nikita-graf/a9d8efd1a78ed03d677e6b3eb4d0924d to your computer and use it in GitHub Desktop.
function fetchDb(cancel) {
return new Promise(function(resolve, reject) {
if (cancel) {
setTimeout(function() {
reject();
console.log('fetchDb cancel');
}, 500);
} else {
setTimeout(function() {
resolve();
console.log('fetchDb done');
}, 2000);
}
});
}
function fulfillIfAnyRejected(promises) {
return Promise.race(promises.map(function(promise, index) {
return promise.then(function() {
return index;
});
}))
.then(function(index) {
promises.splice(index, 1);
if (promises.length) {
return fulfillIfAnyRejected(promises);
}
}, function() {
return 'HAS_REJECTED';
})
}
fulfillIfAnyRejected([
fetchDb(),
fetchDb(true),
fetchDb()
]).then(function(res) {
console.log('done', res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment