Skip to content

Instantly share code, notes, and snippets.

@dance2die
Last active July 19, 2019 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dance2die/992a1157c191eca2b277e1380b323e8b to your computer and use it in GitHub Desktop.
Save dance2die/992a1157c191eca2b277e1380b323e8b to your computer and use it in GitHub Desktop.
// 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)
});
// 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