【发布时间】:2019-08-05 08:29:25
【问题描述】:
当我运行以下程序时
async function functionOne() {
throw new Error('Error here prints the complete stack');
await new Promise((resolve) => {
setTimeout(() => { resolve(); }, 1000);
});
}
async function functionTwo() {
await functionOne();
}
async function functionThree() {
await functionTwo();
}
functionThree()
.catch((error) => {
console.error(error);
});
我得到以下输出,它通过各种调用的函数打印堆栈
Error: Error here prints the complete stack
at functionOne (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:3:9)
at functionTwo (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:11:9)
at functionThree (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:15:9)
at Object.<anonymous> (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:18:1)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Function.Module.runMain (module.js:653:10)
但是当下面程序中的await调用后抛出错误时
async function functionOne() {
await new Promise((resolve) => {
setTimeout(() => { resolve(); }, 1000);
});
throw new Error('Error here prints incomplete stack');
}
async function functionTwo() {
await functionOne();
}
async function functionThree() {
await functionTwo();
}
functionThree()
.catch((error) => {
console.error(error);
});
这是输出
Error: Error here prints incomplete stack
at functionOne (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:7:9)
at <anonymous>
我想了解为什么堆栈跟踪在第二种情况下丢失,但在第一种情况下没有。
【问题讨论】:
-
嗯,异步堆栈跟踪应该解决这个问题。你到底在哪个 node.js 版本上?
-
@Bergi 在我的答案中添加了
--async-stack-traces标志,但我还没有找到支持它的确切版本。我相信它会出现在 Node 12 上 -
根据issue,它被安排在节点 12 上。我发布这个问题更多的是为了理解行为而不是任何直接的问题
标签: javascript node.js error-handling async-await stack-trace