最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Debugging Promise call stacks in vscode - Stack Overflow

programmeradmin3浏览0评论

Is there a way to get the call stack when debugging nodeJS Promises in vscode? I see that support for async call stacks is supported in this GitHub issue but it looks like it pertains to vanilla JS callbacks.

Right now when I'm paused on a breakpoint, the call stack is tiny, even though I know this function is being called from another (couple) of functions.

I'm running on node v6.9.x

EDIT: adding the "protocol": "inspector" attribute to the launch.json config adds some more stackframes, but it's not very helpful:

All of my functions are returning promises, and the function in the screenshot is being called as one of the functions in a Promise.all() call.

Is there a way to get the call stack when debugging nodeJS Promises in vscode? I see that support for async call stacks is supported in this GitHub issue but it looks like it pertains to vanilla JS callbacks.

Right now when I'm paused on a breakpoint, the call stack is tiny, even though I know this function is being called from another (couple) of functions.

I'm running on node v6.9.x

EDIT: adding the "protocol": "inspector" attribute to the launch.json config adds some more stackframes, but it's not very helpful:

All of my functions are returning promises, and the function in the screenshot is being called as one of the functions in a Promise.all() call.

Share Improve this question edited Jul 31, 2018 at 22:18 user4639281 asked Jun 2, 2017 at 23:46 BrDaHaBrDaHa 5,7906 gold badges36 silver badges53 bronze badges 7
  • 1 Do you have "protocol": "inspector" in you launch.json? Unless you already have node v8, default is "legacy" – ccprog Commented Jun 3, 2017 at 1:21
  • I did notice that I get a legacy debugging warning when starting the debugger, but wasn't sure if it was part of the node version or the capabilities of the vscode node debugger – BrDaHa Commented Jun 3, 2017 at 17:17
  • See the details here: code.visualstudio./updates/v1_11#_node-debugging – ccprog Commented Jun 3, 2017 at 17:38
  • I've added the config you specified, but I'm not getting the same kind of output as illustrated in the example. I updated the question – BrDaHa Commented Jun 5, 2017 at 21:08
  • Did you try to add the breakpoint into the function where the promise itself returns? You should be able to run until the callback returns and then jump into the breakpoint which was in the function of the promise. – nmanh Commented Mar 26, 2018 at 16:18
 |  Show 2 more ments

1 Answer 1

Reset to default 2

First of all, the callstack as shown is correct in the way that these are the functions that are actually on the stack. When a Promise gets created around an asynchronous callback, and .then handlers get attached, synchronous execution ends and the callstack gets unwrapped. When the callback calls back somewhen, it resolves the Promise and the .then handlers get executed. At that point the callstack only contains the function passed to .then.

Now in a lot of scenarios the Promise chain is flat, and goes the other way it was created:

function a() {
   return Promise.resolve(1).then(it => it + 1); // 1
}

function b() {
   return a().then(it => it + 1); // 2
}

At the point above when the first .then callback (1) executes, the only attached callback is (2), and as such he engine could generate an "async stacktrace", showing at which functions the Promise chain continues.

Now for plain promises resolving these chains just to generate a stacktrace is a lot of overhead, however for async functions that await Promises it is pretty straightforward. Thus since NodeJS v12 (and in modern browsers) if you write the above like this instead:

async function a() {
  const result = (await 1);
  _log_stack();
  return result + 1;
}

async function b() {
  return (await a()) + 1;
}
     
b();
     
function _log_stack() { console.log((new Error()).stack); }

then your debugger will show a proper async stacktrace (which gets generated as described above). You can find a more in depth explanation here.

发布评论

评论列表(0)

  1. 暂无评论