Created
April 18, 2019 10:46
-
-
Save atesztoth/f56c14b90664f27537bde7fa7d693fd4 to your computer and use it in GitHub Desktop.
Failproof promise all
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
console.time('start') | |
async function myFunction() { | |
const success = Promise.resolve('Yaaay') | |
const promise = new Promise((_, reject) => setTimeout(() => { | |
console.timeEnd('start') | |
reject('Rejection reason') | |
}, 200)) | |
const autoCatchedPromise = promise.catch(error => { | |
console.info('Error caught:', error) | |
return null | |
}) | |
const otherVariable = await Promise.all([success, autoCatchedPromise]) // change it to the original promise, and it'll reach the error banch. | |
console.info(autoCatchedPromise) | |
console.info(otherVariable) | |
} | |
myFunction().then(value => console.info('value', value)).catch(error => console.error('This should never have been reached. Error:', error)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment