Skip to content

Instantly share code, notes, and snippets.

@cakoose
Created July 18, 2020 02:17
Show Gist options
  • Save cakoose/8bc94eef7890a234bcdf5d607bf175e9 to your computer and use it in GitHub Desktop.
Save cakoose/8bc94eef7890a234bcdf5d607bf175e9 to your computer and use it in GitHub Desktop.
// Pretend this is the non-async library code we're calling in to.
function gap() {
return new Promise(resolve => setTimeout(resolve, 1));
}
function f4() {
return new Promise((resolve, reject) => {
gap().then(() => reject(new Error('from f4')));
});
}
// This is our own native async code.
async function f3() {
await gap();
try {
await f4();
} catch (err) {
// Instead of relaying the original Error, create a new one.
// Since we're part of a chain of native async functions, the
// new Error should have a good stack trace.
throw new Error('from f3');
}
}
async function f2() {
await gap();
await f3();
}
async function f1() {
await gap();
await f2();
}
f1()
.then(() => console.log('done'))
.catch(err => { console.log(err.stack); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment