Created
March 1, 2025 21:16
-
-
Save stephenh/0b4511b8766b3ea8289312623cfb9d39 to your computer and use it in GitHub Desktop.
Stacks Example
This file contains 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
allAwaitsSync | |
10 | } | |
11 | async function bar() { | |
12 | await zaz(); | |
13 | } | |
14 | async function zaz() { | |
15 | throw new Error("oops"); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:15:11 | |
at zaz (/home/stephen/other/blog/public/files/stacks.mjs:14:24) | |
at /home/stephen/other/blog/public/files/stacks.mjs:12:11 | |
at bar (/home/stephen/other/blog/public/files/stacks.mjs:11:24) | |
at /home/stephen/other/blog/public/files/stacks.mjs:9:11 | |
at foo (/home/stephen/other/blog/public/files/stacks.mjs:8:24) | |
at /home/stephen/other/blog/public/files/stacks.mjs:17:9 | |
at allAwaitsSync (/home/stephen/other/blog/public/files/stacks.mjs:7:29) | |
at /home/stephen/other/blog/public/files/stacks.mjs:150:11 | |
allPromisesSync | |
24 | function bar() { | |
25 | return zaz(); | |
26 | } | |
27 | function zaz() { | |
28 | return new Promise(function handle(resolve, reject) { | |
29 | reject(new Error("oops")); | |
^ | |
error: oops | |
at handle (/home/stephen/other/blog/public/files/stacks.mjs:29:14) | |
at new Promise (:1:11) | |
at zaz (/home/stephen/other/blog/public/files/stacks.mjs:28:12) | |
at /home/stephen/other/blog/public/files/stacks.mjs:150:11 | |
allAwaitsTimeout | |
1 | function badDatabaseCall() { | |
2 | return new Promise((resolve, reject) => { | |
3 | setTimeout(() => reject(new Error("oops")), 50); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:3:29 | |
allPromisesTimeout | |
1 | function badDatabaseCall() { | |
2 | return new Promise((resolve, reject) => { | |
3 | setTimeout(() => reject(new Error("oops")), 50); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:3:29 | |
allAwaitsTimeoutAppend | |
1 | function badDatabaseCall() { | |
2 | return new Promise((resolve, reject) => { | |
3 | setTimeout(() => reject(new Error("oops")), 50); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:3:33 | |
at zaz (/home/stephen/other/blog/public/files/stacks.mjs:76:16) | |
allAwaitsTimeoutLambdaAppend | |
1 | function badDatabaseCall() { | |
2 | return new Promise((resolve, reject) => { | |
3 | setTimeout(() => reject(new Error("oops")), 50); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:3:33 | |
at /home/stephen/other/blog/public/files/stacks.mjs:92:24 | |
allPromisesTimeoutAppend | |
1 | function badDatabaseCall() { | |
2 | return new Promise((resolve, reject) => { | |
3 | setTimeout(() => reject(new Error("oops")), 50); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:3:33 | |
at zaz (/home/stephen/other/blog/public/files/stacks.mjs:123:15) | |
allAsyncOneMissingAwaitTimeoutAppend | |
1 | function badDatabaseCall() { | |
2 | return new Promise((resolve, reject) => { | |
3 | setTimeout(() => reject(new Error("oops")), 50); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:3:33 | |
at zaz (/home/stephen/other/blog/public/files/stacks.mjs:159:10) | |
allAwaitsTimeoutCause | |
95 | async function bar() { | |
96 | await zaz(); | |
97 | } | |
98 | async function zaz() { | |
99 | return badDatabaseCall().catch((err) => { | |
100 | throw new Error(err.message, { cause: err }); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:100:13 | |
1 | function badDatabaseCall() { | |
2 | return new Promise((resolve, reject) => { | |
3 | setTimeout(() => reject(new Error("oops")), 50); | |
^ | |
error: oops | |
at /home/stephen/other/blog/public/files/stacks.mjs:3:29 |
This file contains 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
allAwaitsSync | |
Error: oops | |
at zaz (file:///home/stephen/other/blog/public/files/stacks.mjs:15:11) | |
at bar (file:///home/stephen/other/blog/public/files/stacks.mjs:12:11) | |
at foo (file:///home/stephen/other/blog/public/files/stacks.mjs:9:11) | |
at allAwaitsSync (file:///home/stephen/other/blog/public/files/stacks.mjs:17:9) | |
at file:///home/stephen/other/blog/public/files/stacks.mjs:150:11 | |
at ModuleJob.run (node:internal/modules/esm/module_job:192:25) | |
at async DefaultModuleLoader.import (node:internal/modules/esm/loader:228:24) | |
at async loadESM (node:internal/process/esm_loader:40:7) | |
at async handleMainPromise (node:internal/modules/run_main:66:12) | |
allPromisesSync | |
Error: oops | |
at handle (file:///home/stephen/other/blog/public/files/stacks.mjs:29:14) | |
at new Promise (<anonymous>) | |
at zaz (file:///home/stephen/other/blog/public/files/stacks.mjs:28:12) | |
at bar (file:///home/stephen/other/blog/public/files/stacks.mjs:25:12) | |
at foo (file:///home/stephen/other/blog/public/files/stacks.mjs:22:12) | |
at allPromisesSync (file:///home/stephen/other/blog/public/files/stacks.mjs:32:10) | |
at file:///home/stephen/other/blog/public/files/stacks.mjs:150:11 | |
allAwaitsTimeout | |
Error: oops | |
at Timeout._onTimeout (file:///home/stephen/other/blog/public/files/stacks.mjs:3:29) | |
at listOnTimeout (node:internal/timers:573:17) | |
at process.processTimers (node:internal/timers:514:7) | |
allPromisesTimeout | |
Error: oops | |
at Timeout._onTimeout (file:///home/stephen/other/blog/public/files/stacks.mjs:3:29) | |
at listOnTimeout (node:internal/timers:573:17) | |
at process.processTimers (node:internal/timers:514:7) | |
allAwaitsTimeoutAppend | |
Error: oops | |
at Timeout._onTimeout (file:///home/stephen/other/blog/public/files/stacks.mjs:3:29) | |
at listOnTimeout (node:internal/timers:573:17) | |
at process.processTimers (node:internal/timers:514:7) | |
at zaz (file:///home/stephen/other/blog/public/files/stacks.mjs:70:30) | |
at async bar (file:///home/stephen/other/blog/public/files/stacks.mjs:66:5) | |
at async foo (file:///home/stephen/other/blog/public/files/stacks.mjs:63:5) | |
at async allAwaitsTimeoutAppend (file:///home/stephen/other/blog/public/files/stacks.mjs:73:3) | |
at async file:///home/stephen/other/blog/public/files/stacks.mjs:150:5 | |
allAwaitsTimeoutLambdaAppend | |
Error: oops | |
at Timeout._onTimeout (file:///home/stephen/other/blog/public/files/stacks.mjs:3:29) | |
at listOnTimeout (node:internal/timers:573:17) | |
at process.processTimers (node:internal/timers:514:7) | |
at file:///home/stephen/other/blog/public/files/stacks.mjs:85:30 | |
at async bar (file:///home/stephen/other/blog/public/files/stacks.mjs:81:5) | |
at async foo (file:///home/stephen/other/blog/public/files/stacks.mjs:78:5) | |
at async allAwaitsTimeoutLambdaAppend (file:///home/stephen/other/blog/public/files/stacks.mjs:88:3) | |
at async file:///home/stephen/other/blog/public/files/stacks.mjs:150:5 | |
allPromisesTimeoutAppend | |
Error: oops | |
at Timeout._onTimeout (file:///home/stephen/other/blog/public/files/stacks.mjs:3:29) | |
at listOnTimeout (node:internal/timers:573:17) | |
at process.processTimers (node:internal/timers:514:7) | |
at zaz (file:///home/stephen/other/blog/public/files/stacks.mjs:115:30) | |
at async file:///home/stephen/other/blog/public/files/stacks.mjs:150:5 | |
allAsyncOneMissingAwaitTimeoutAppend | |
Error: oops | |
at Timeout._onTimeout (file:///home/stephen/other/blog/public/files/stacks.mjs:3:29) | |
at listOnTimeout (node:internal/timers:573:17) | |
at process.processTimers (node:internal/timers:514:7) | |
at zaz (file:///home/stephen/other/blog/public/files/stacks.mjs:130:30) | |
at async foo (file:///home/stephen/other/blog/public/files/stacks.mjs:123:5) | |
at async allAsyncOneMissingAwaitTimeoutAppend (file:///home/stephen/other/blog/public/files/stacks.mjs:133:3) | |
at async file:///home/stephen/other/blog/public/files/stacks.mjs:150:5 | |
allAwaitsTimeoutCause | |
Error: oops | |
at file:///home/stephen/other/blog/public/files/stacks.mjs:100:13 | |
at async bar (file:///home/stephen/other/blog/public/files/stacks.mjs:96:5) | |
at async foo (file:///home/stephen/other/blog/public/files/stacks.mjs:93:5) | |
at async allAwaitsTimeoutCause (file:///home/stephen/other/blog/public/files/stacks.mjs:103:3) | |
at async file:///home/stephen/other/blog/public/files/stacks.mjs:150:5 { | |
[cause]: Error: oops | |
at Timeout._onTimeout (file:///home/stephen/other/blog/public/files/stacks.mjs:3:29) | |
at listOnTimeout (node:internal/timers:573:17) | |
at process.processTimers (node:internal/timers:514:7) | |
} |
This file contains 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
function badDatabaseCall() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => reject(new Error("oops")), 50); | |
}); | |
} | |
async function allAwaitsSync() { | |
async function foo() { | |
await bar(); | |
} | |
async function bar() { | |
await zaz(); | |
} | |
async function zaz() { | |
throw new Error("oops"); | |
} | |
await foo(); | |
} | |
function allPromisesSync() { | |
function foo() { | |
return bar(); | |
} | |
function bar() { | |
return zaz(); | |
} | |
function zaz() { | |
return new Promise(function handle(resolve, reject) { | |
reject(new Error("oops")); | |
}); | |
} | |
return foo(); | |
} | |
async function allAwaitsTimeout() { | |
async function foo() { | |
await bar(); | |
} | |
async function bar() { | |
await zaz(); | |
} | |
async function zaz() { | |
return badDatabaseCall(); | |
} | |
await foo(); | |
} | |
function allPromisesTimeout() { | |
function foo() { | |
return bar(); | |
} | |
function bar() { | |
return zaz(); | |
} | |
function zaz() { | |
return badDatabaseCall(); | |
} | |
return foo(); | |
} | |
async function allAwaitsTimeoutAppend() { | |
async function foo() { | |
await bar(); | |
} | |
async function bar() { | |
await zaz(); | |
} | |
async function zaz() { | |
return badDatabaseCall().catch(function zaz(err) { | |
throw appendStack(err, new Error()); | |
}); | |
} | |
await foo(); | |
} | |
async function allAwaitsTimeoutLambdaAppend() { | |
async function foo() { | |
await bar(); | |
} | |
async function bar() { | |
await zaz(); | |
} | |
async function zaz() { | |
return badDatabaseCall().catch((err) => { | |
throw appendStack(err, new Error()); | |
}); | |
} | |
await foo(); | |
} | |
async function allAwaitsTimeoutCause() { | |
async function foo() { | |
await bar(); | |
} | |
async function bar() { | |
await zaz(); | |
} | |
async function zaz() { | |
return badDatabaseCall().catch((err) => { | |
throw new Error(err.message, { cause: err }); | |
}); | |
} | |
await foo(); | |
} | |
function allPromisesTimeoutAppend() { | |
function foo() { | |
return bar(); | |
} | |
function bar() { | |
return zaz(); | |
} | |
function zaz() { | |
return badDatabaseCall().catch(function zaz(err) { | |
throw appendStack(err, new Error()); | |
}); | |
} | |
return foo(); | |
} | |
async function allAsyncOneMissingAwaitTimeoutAppend() { | |
async function foo() { | |
await bar(); | |
} | |
async function bar() { | |
return zaz(); | |
} | |
async function zaz() { | |
return badDatabaseCall().catch(function zaz(err) { | |
throw appendStack(err, new Error()); | |
}); | |
} | |
await foo(); | |
} | |
for (const fn of [ | |
allAwaitsSync, | |
allPromisesSync, | |
allAwaitsTimeout, | |
allPromisesTimeout, | |
allAwaitsTimeoutAppend, | |
allAwaitsTimeoutLambdaAppend, | |
allPromisesTimeoutAppend, | |
allAsyncOneMissingAwaitTimeoutAppend, | |
allAwaitsTimeoutCause, | |
]) { | |
console.log(""); | |
console.log(fn.name); | |
try { | |
await fn(); | |
} catch (err) { | |
console.log(err); | |
} | |
} | |
export function appendStack(err, dummy) { | |
if (err && typeof err === "object" && "stack" in err) { | |
err.stack += dummy.stack.replace(/.*\n/, "\n"); | |
} | |
return err; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment