Last active
July 19, 2019 13:11
-
-
Save dance2die/992a1157c191eca2b277e1380b323e8b 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
// Example #1 | |
Promise.any([ | |
Promise.reject('✗'), | |
Promise.reject('✗'), | |
Promise.resolve('✓'), | |
Promise.reject('✗'), | |
]).then(function(value) { | |
console.log(`You win at life`, value) | |
}); | |
// Example #2 | |
// You get the first fulfilled value | |
Promise.any([ | |
new Promise((_, reject) => setTimeout(reject, 10, '✗')), | |
new Promise((_, reject) => setTimeout(reject, 20, '✗')), | |
new Promise((_, reject) => setTimeout(reject, 30, '✗')), | |
new Promise(resolve => setTimeout(resolve, 100, 'I got a job!')), | |
new Promise(resolve => setTimeout(resolve, 1000, 'I could have gotten a better job!')), | |
]).then(function(value) { | |
console.log(value) | |
}); | |
// Example #3 | |
// You get all rejection reasons | |
Promise.any([ | |
Promise.reject('✗'), | |
Promise.reject('✗'), | |
]).catch(function(reasons) { | |
console.log(`Didn't get any offers...`, reasons) | |
}); |
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
// Note that the result of Example #3 is printed first because of `setTimeout` in example #2 | |
// Result of Example #1 | |
You win at life ✓ | |
// Result of Example #3 | |
Didn't get any offers... [ '✗', '✗' ] | |
// Result of Example #2 | |
I got a job! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment