Skip to content

Instantly share code, notes, and snippets.

@atesztoth
Created April 18, 2019 10:46
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 atesztoth/f56c14b90664f27537bde7fa7d693fd4 to your computer and use it in GitHub Desktop.
Save atesztoth/f56c14b90664f27537bde7fa7d693fd4 to your computer and use it in GitHub Desktop.
Failproof promise all
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