Created
June 28, 2018 16:28
-
-
Save jsumners/f85f6ad7246d649781f6eea21b389dc0 to your computer and use it in GitHub Desktop.
A simple pattern for working with async/await
This file contains hidden or 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
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