简体   繁体   English

使用 Promise.then 与 async-await 时丢失堆栈跟踪

[英]lose stack trace when using Promise.then vs. async-await

I have the following snippet implemented with Promise.then :我使用Promise.then实现了以下代码段:

function b() {
  return Promise.resolve()
}


function c() {
   throw new Error('error here')
}

const a = () => {
    b().then(() => c());
};


a()

The stack trace output from the error only included function c错误的堆栈跟踪输出仅包括函数c

VM57:6 Uncaught (in promise) Error: 343
    at c (<anonymous>:6:9)
    at <anonymous>:10:17

However, if I switch this to async-await as in:但是,如果我将其切换为 async-await,如下所示:

function b() {
  return Promise.resolve()
}


function c() {
   throw new Error('error here')
}

const a = async () => {
    await b()
    c()
};


a()

This time the stack trace included the whole call stack with function a这次堆栈跟踪包括了带有函数a的整个调用堆栈

Uncaught (in promise) Error: 343
    at c (<anonymous>:6:9)
    at a (<anonymous>:3:5)

I tested this in Chrome dev console.我在 Chrome 开发控制台中对此进行了测试。 I wonder what caused the difference and does this mean one should stick with async-await for better debugbility?我想知道是什么导致了这种差异,这是否意味着应该坚持使用async-await以获得更好的可调试性? Also are there any resources I can learn more about this difference?还有什么资源可以让我了解更多关于这种差异的信息吗?

This is expected.这是意料之中的。

In the then version, the function a has ran to completion before the then callback executes.then版本中,函数athen回调执行之前已经运行完成。 That anonymous callback is the only function on the call stack at that moment.该匿名回调是当时调用堆栈上的唯一函数。 When c is called, and the rejection occurs, a is not on the call stack.当调用c并发生拒绝时, a不在调用堆栈上。

In the await version, the function a is first suspended (it returns a promise), but then when b() resolves, the execution context of a is restored and execution of a resumes.await版本中,函数a首先被挂起(它返回一个 promise),但是当b()解析时,a 的执行上下文被恢复并且aa恢复。 So when the rejection occurs in c , a is on the call stack.因此,当c中发生拒绝时, a在调用堆栈上。

Its anonymous because its called inside an anonymous function: () => c() .它是匿名的,因为它在匿名函数内部调用: () => c() In the other example its called inside a在另一个示例中,它在内部a

If you name your callback function, you will see the name inside the error log:如果您命名回调函数,您将在错误日志中看到名称:

const a = () => {
  b().then(function test() {
    c();
  });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 Node.js 中使用 async-await 时会丢失堆栈跟踪? - Why do I lose stack trace when using async-await in Node.js? 使用 async-await 时如何将 Resolve 回调传递给 Promise? - How to pass Resolve callback into a Promise when using async-await? Javascript异步函数的“等待”的正确心智模型:生成器的“产量”与“ promise.then()”? - Correct mental model for a Javascript async function's 'await': generator's 'yield' vs. 'promise.then()'? 得到'承诺{<pending> }' 使用 async-await 执行 tcp 客户端时的消息</pending> - Getting 'Promise { <pending> }' message when using async-await to perform tcp client Javascript:使用承诺时的奇怪行为,异步等待,返回“承诺<pending> ”</pending> - Javascript: Strange behavior when using promises, async-await, returns “Promise <pending>” Javascript:promise 链 vs. async/await? - Javascript : promise chain vs. async/await? Promise.then(()=&gt;function()) 与 Promise.then(function) - Promise.then(()=>function()) vs. Promise.then(function) 使用“异步等待”模式时,Mocha单元测试尚未完成 - Mocha unit test not finalized when using the 'async-await' pattern 异步/等待vs承诺 - Async/await vs Promise 如何在循环中等待 promise 完成? (不使用异步等待) - How do I wait for a promise to finish inside a loop? (without using async-await)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM