Last active
October 16, 2019 00:30
-
-
Save dance2die/e6f491a82eb10a28f3743f68f5c9d4fe 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
const allRejectedPromises = [ | |
Promise.reject('π #1'), | |
Promise.reject('π #2'), | |
Promise.reject('π #3'), | |
] | |
Promise.allSettled(allRejectedPromises) | |
.then(badApples => | |
console.log(`We can't sell any of these apples...`, badApples)) | |
.catch(error => console.error('This should never occur')) | |
const promisesWithoutReject = [ | |
Promise.resolve('π #1'), | |
'π #2', | |
new Promise((resolve, reject) => setTimeout(resolve, 100, 'π #3')) | |
] | |
Promise.allSettled(promisesWithoutReject) | |
.then(apples => console.log(`We can sell all these good apples`, apples.map(_=>_.value))) | |
const promisesWithOneReject = [ | |
Promise.resolve('π #1'), | |
new Promise((_, reject) => setTimeout(reject, 10, 'π #2')), | |
'π #3', | |
new Promise((_, reject) => setTimeout(reject, 100, 'π #4')) | |
] | |
Promise.allSettled(promisesWithOneReject) | |
.then(apples => { | |
const badApples = apples.filter(apple => apple.status === 'rejected').map(_ => _.reason) | |
const goodApples = apples.filter(apple => apple.status === 'fulfilled').map(_ => _.value) | |
console.log(`Let's throw out`, badApples, `and sell the rest`, goodApples) | |
}) |
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
We can't sell any of these apples... | |
[ { status: 'rejected', reason: 'π #1' }, | |
{ status: 'rejected', reason: 'π #2' }, | |
{ status: 'rejected', reason: 'π #3' } ] | |
We can sell all these good apples [ 'π #1', 'π #2', 'π #3' ] | |
Let's throw out [ 'π #2', 'π #4' ] and sell the rest [ 'π #1', 'π #3' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment