Created
April 11, 2017 16:39
-
-
Save nikita-graf/a9d8efd1a78ed03d677e6b3eb4d0924d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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