https://itnext.io/error-handling-with-async-await-in-js-26c3f20bc06a
async function thisThrows() {
throw new Error("Thrown from thisThrows()");
}
try {
thisThrows();
} catch (e) {
console.error(e);
} finally {
console.log('We do cleanup here');
}
We do cleanup here
Promise {
<rejected> Error: Thrown from thisThrows()
at thisThrows (REPL27:2:11)
at REPL35:2:5
at Script.runInThisContext (node:vm:122:12)
at REPLServer.defaultEval (node:repl:593:29)
at bound (node:domain:433:15)
at REPLServer.runBound [as eval] (node:domain:444:12)
at REPLServer.onLine (node:repl:923:10)
at REPLServer.emit (node:events:526:35)
at REPLServer.emit (node:domain:489:12)
at [_onLine] [as _onLine] (node:internal/readline/interface:416:12),
[Symbol(async_id_symbol)]: 86,
[Symbol(trigger_async_id_symbol)]: 6
}
> Uncaught Error: Thrown from thisThrows()
at thisThrows (REPL27:2:11)
at REPL35:2:5
at Script.runInThisContext (node:vm:122:12)
at REPLServer.defaultEval (node:repl:593:29)
at bound (node:domain:433:15)
at REPLServer.runBound [as eval] (node:domain:444:12)
at REPLServer.onLine (node:repl:923:10)
at REPLServer.emit (node:events:526:35)
at REPLServer.emit (node:domain:489:12)
at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
async function thisThrows() {
throw new Error("Thrown from thisThrows()");
}
async function run() {
try {
await thisThrows();
} catch (e) {
console.error(e);
} finally {
console.log('We do cleanup here');
}
}
run();
Promise {
<pending>,
[Symbol(async_id_symbol)]: 132,
[Symbol(trigger_async_id_symbol)]: 6
}
> Error: Thrown from thisThrows()
at thisThrows (REPL43:2:11)
at run (REPL53:3:15)
at REPL55:1:1
at Script.runInThisContext (node:vm:122:12)
at REPLServer.defaultEval (node:repl:593:29)
at bound (node:domain:433:15)
at REPLServer.runBound [as eval] (node:domain:444:12)
at REPLServer.onLine (node:repl:923:10)
at REPLServer.emit (node:events:526:35)
at REPLServer.emit (node:domain:489:12)
We do cleanup here
async function thisThrows() {
throw new Error("Thrown from thisThrows()");
}
thisThrows()
.catch(console.error)
.then(() => console.log('We do cleanup here'));
Promise {
<pending>,
[Symbol(async_id_symbol)]: 266,
[Symbol(trigger_async_id_symbol)]: 265
}
> Error: Thrown from thisThrows()
at thisThrows (REPL78:2:11)
at REPL86:1:1
at Script.runInThisContext (node:vm:122:12)
at REPLServer.defaultEval (node:repl:593:29)
at bound (node:domain:433:15)
at REPLServer.runBound [as eval] (node:domain:444:12)
at REPLServer.onLine (node:repl:923:10)
at REPLServer.emit (node:events:526:35)
at REPLServer.emit (node:domain:489:12)
at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
We do cleanup here