Skip to content

Instantly share code, notes, and snippets.

@strboul
Created March 29, 2021 14:31
Show Gist options
  • Save strboul/6c6f35df5fa96a6ee33e95d2a732af9c to your computer and use it in GitHub Desktop.
Save strboul/6c6f35df5fa96a6ee33e95d2a732af9c to your computer and use it in GitHub Desktop.
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const queryDb = async (success) => {
await sleep(3000);
if (!success) {
throw new TypeError();
}
console.log("database queried");
};
const init = async () => {
console.log("before");
await queryDb(true);
console.log("after");
console.log("before 2");
await queryDb(false);
console.log("after 2");
};
init();
// before
// database queried
// after
// before 2
// /home/myazici/async.js:10
// throw new TypeError();
// ^
//
// TypeError
// at queryDb (/home/myazici/async.js:10:11)
// at async init (/home/myazici/async.js:21:3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment