Skip to content

Instantly share code, notes, and snippets.

@jsumners
Created June 28, 2018 16:28
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 jsumners/f85f6ad7246d649781f6eea21b389dc0 to your computer and use it in GitHub Desktop.
Save jsumners/f85f6ad7246d649781f6eea21b389dc0 to your computer and use it in GitHub Desktop.
A simple pattern for working with async/await
async function doFoo () {
let bar
try {
bar = await something_that_can_throw('a value')
} catch (error) {
return {error}
}
return {value: bar}
}
async function main () {
const { error: fooError, value: fooVal } = await doFoo()
if (fooError) {
console.error('oops: %s', fooError.message)
process.exit(1)
}
console.log('success: %j', fooVal)
}
main().then(() => {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment