Skip to content

Instantly share code, notes, and snippets.

@zignd
Last active May 10, 2020 23:54
Show Gist options
  • Select an option

  • Save zignd/98807b94a635990a227943596935dafc to your computer and use it in GitHub Desktop.

Select an option

Save zignd/98807b94a635990a227943596935dafc to your computer and use it in GitHub Desktop.
async function openDatabaseConnection() {
const connection = {
isOpen: true,
hasPermissionToDelete: false
}
return connection;
}
async function example() {
const connection = await openDatabaseConnection();
if (!connection.hasPermissionToDelete) {
throw new Error("can't use connection, doesn't have permission to delete")
}
// ...
}
example().then(() => {
console.log("Operation successfully completed")
}).catch((err) => {
console.error(new Error(`operation failed to complete: ${err.message}`))
})
// $ node throw-async.js
// Error: operation failed to complete: can't use connection, doesn't have permission to delete
// at /home/zignd/code/error-handling-nodejs/throw-async.js:20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment