Skip to content

Instantly share code, notes, and snippets.

@agm1984
Created April 10, 2018 05:04
Show Gist options
  • Save agm1984/193e941ba3013fad19dc3553104c0b46 to your computer and use it in GitHub Desktop.
Save agm1984/193e941ba3013fad19dc3553104c0b46 to your computer and use it in GitHub Desktop.
JavaScript: is return await needed? sometimes.
// INCLUDING AWAIT DELEGATES TO PARENT
const dontBubbleUp = async () => {
const samplerPack = async () => {
throw new Error('BAD')
}
try {
return await samplerPack()
} catch (e) {
throw new Error('handling rejection upstream in parent')
}
}
dontBubbleUp()
.then(res => console.log('End of chain reached.', res))
.catch(err => console.log('YOLO TEST 1:\n', 'End of chain reached.', err))
// EXCLUDING AWAIT HANDLES FIRST AT CALL SIGHT
const bubbleUp = async () => {
const newSamplerPack = async () => {
throw new Error('handling error in newSamplerPack first')
}
try {
return newSamplerPack()
} catch (e) {
throw new Error('handling error locally')
}
}
bubbleUp()
.then(res => console.log('End of chain reached.', res))
.catch(err => console.log('YOLO TEST 2:\n', 'End of chain reached.', err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment