Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
see what stack traces look like for nested async await calls
'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>
...
*/
'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>
...
*/
'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
You can’t perform that action at this time.