Created
May 10, 2018 14:40
-
-
Save pmuellr/3e0b4ffacb1d6b5af10cd357ed9c84ee to your computer and use it in GitHub Desktop.
see what stack traces look like for nested async await calls
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
'use strict' | |
const states = 'abcde'.split('') // [ 'a', 'b', ... ] | |
async function main () { | |
for (let state of states) { | |
try { | |
await a(state) | |
} catch (err) { | |
console.log(`state: ${state}`, err) | |
} | |
console.log('') | |
} | |
} | |
async function a (state) { | |
try { | |
await b(state) | |
} catch (err) { | |
console.log('error in a() await on b()') | |
throw err | |
} | |
if (state === 'a') throw new Error('a') | |
} | |
async function b (state) { | |
try { | |
await c(state) | |
} catch (err) { | |
console.log('error in b() await on c()') | |
throw err | |
} | |
if (state === 'b') throw new Error('b') | |
} | |
async function c (state) { | |
try { | |
await d(state) | |
} catch (err) { | |
console.log('error in c() await on d()') | |
throw err | |
} | |
try { | |
await e(state) | |
} catch (err) { | |
console.log('error in c() await on e()') | |
throw err | |
} | |
if (state === 'c') throw new Error('c') | |
} | |
async function d (state) { | |
if (state === 'd') throw new Error('d') | |
} | |
async function e (state) { | |
if (state === 'e') throw new Error('e') | |
} | |
main() | |
/* output: | |
state: a Error: a | |
at a (/path/to/throwing-catching-logging.js:23:28) | |
at <anonymous> | |
... | |
error in a() await on b() | |
state: b Error: b | |
at b (/path/to/throwing-catching-logging.js:33:28) | |
at <anonymous> | |
... | |
error in b() await on c() | |
error in a() await on b() | |
state: c Error: c | |
at c (/path/to/throwing-catching-logging.js:51:28) | |
at <anonymous> | |
... | |
error in c() await on d() | |
error in b() await on c() | |
error in a() await on b() | |
state: d Error: d | |
at d (/path/to/throwing-catching-logging.js:55:28) | |
at c (/path/to/throwing-catching-logging.js:38:11) | |
at b (/path/to/throwing-catching-logging.js:28:11) | |
at a (/path/to/throwing-catching-logging.js:18:11) | |
at main (/path/to/throwing-catching-logging.js:8:13) | |
at <anonymous> | |
... | |
error in c() await on e() | |
error in b() await on c() | |
error in a() await on b() | |
state: e Error: e | |
at e (/path/to/throwing-catching-logging.js:59:28) | |
at c (/path/to/throwing-catching-logging.js:45:11) | |
at <anonymous> | |
... | |
*/ |
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
'use strict' | |
const states = 'abcde'.split('') // [ 'a', 'b', ... ] | |
async function main () { | |
for (let state of states) { | |
try { | |
await a(state) | |
} catch (err) { | |
console.log(`state: ${state}`, err) | |
} | |
} | |
} | |
async function a (state) { | |
try { | |
await b(state) | |
} catch (err) { | |
throw err | |
} | |
if (state === 'a') throw new Error('a') | |
} | |
async function b (state) { | |
try { | |
await c(state) | |
} catch (err) { | |
throw err | |
} | |
if (state === 'b') throw new Error('b') | |
} | |
async function c (state) { | |
try { | |
await d(state) | |
} catch (err) { | |
throw err | |
} | |
try { | |
await e(state) | |
} catch (err) { | |
throw err | |
} | |
if (state === 'c') throw new Error('c') | |
} | |
async function d (state) { | |
if (state === 'd') throw new Error('d') | |
} | |
async function e (state) { | |
if (state === 'e') throw new Error('e') | |
} | |
main() | |
/* output: | |
state: a Error: a | |
at a (/path/to/throwing-catching.js:21:28) | |
at <anonymous> | |
... | |
state: b Error: b | |
at b (/path/to/throwing-catching.js:30:28) | |
at <anonymous> | |
... | |
state: c Error: c | |
at c (/path/to/throwing-catching.js:46:28) | |
at <anonymous> | |
... | |
state: d Error: d | |
at d (/path/to/throwing-catching.js:50:28) | |
at c (/path/to/throwing-catching.js:35:11) | |
at b (/path/to/throwing-catching.js:26:11) | |
at a (/path/to/throwing-catching.js:17:11) | |
at main (/path/to/throwing-catching.js:8:13) | |
at <anonymous> | |
... | |
state: e Error: e | |
at e (/path/to/throwing-catching.js:54:28) | |
at c (/path/to/throwing-catching.js:41:11) | |
at <anonymous> | |
... | |
*/ |
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
'use strict' | |
const states = 'abcde'.split('') // [ 'a', 'b', ... ] | |
async function main () { | |
for (let state of states) { | |
try { | |
await a(state) | |
} catch (err) { | |
console.log(`state: ${state}`, err) | |
} | |
} | |
} | |
async function a (state) { | |
await b(state) | |
if (state === 'a') throw new Error('a') | |
} | |
async function b (state) { | |
await c(state) | |
if (state === 'b') throw new Error('b') | |
} | |
async function c (state) { | |
await d(state) | |
await e(state) | |
if (state === 'c') throw new Error('c') | |
} | |
async function d (state) { | |
if (state === 'd') throw new Error('d') | |
} | |
async function e (state) { | |
if (state === 'e') throw new Error('e') | |
} | |
main() | |
/* output: | |
state: a Error: a | |
at a (/path/to/throwing.js:17:28) | |
at <anonymous> | |
... | |
state: b Error: b | |
at b (/path/to/throwing.js:22:28) | |
at <anonymous> | |
... | |
state: c Error: c | |
at c (/path/to/throwing.js:28:28) | |
at <anonymous> | |
... | |
state: d Error: d | |
at d (/path/to/throwing.js:32:28) | |
at c (/path/to/throwing.js:26:9) | |
at b (/path/to/throwing.js:21:9) | |
at a (/path/to/throwing.js:16:9) | |
at main (/path/to/throwing.js:8:13) | |
at <anonymous> | |
... | |
state: e Error: e | |
at e (/path/to/throwing.js:36:28) | |
at c (/path/to/throwing.js:27:9) | |
at <anonymous> | |
... | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment