Last active
June 17, 2024 15:04
-
-
Save lac5/938bb3d4194f85721309188b668e67d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Promise.any is the inverse of Promise.all | |
* | |
* Promise.all = reject first error or resolve all successes | |
* Promise.any = resolve first success or reject all errors | |
*/ | |
Promise.any = function(promises) { | |
return Promise.all(promises.map(p => { | |
return p.then( | |
resolve => { throw resolve; }, | |
reject => reject | |
); | |
})).then( | |
rejects => { throw rejects }, | |
resolve => resolve | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment