且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Visual Studio 代码中调试异步/等待?

更新时间:2022-04-12 21:49:06

截至目前,2019 年最新的 VSCode 支持 async/await 调试,只是想分享解决方案,防止 vscode Step into"(按 f11)进入在调试 nodejs 应用程序期间通过async_hooks.js"和inspector_async_hook.js"文件进行异步/等待方法.

As now, in 2019, latest VSCode supporting async/await debug, just would like to share solution to prevent vscode to "Step into" (by f11) into async/await method through the "async_hooks.js" and "inspector_async_hook.js" files during debug nodejs applications.

方法:

1) 在 vscode 中按 ctrl+p 并输入>launch",选择open launch.json"

1) press ctrl+p in vscode and type ">launch", select "open launch.json"

2) 打开launch.json"后,只需添加到配置部分:

2) after opening "launch.json", just add to configuration section :

"skipFiles": [
    "inspector_async_hook.js",
    "async_hooks.js"
  ]

或更通用的版本:

"skipFiles": ["<node_internals>/**"]

所以你最终的 json 应该看起来像这里:

So you final json should looks something like here:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceFolder}\index.js",
      "skipFiles": ["<node_internals>/**"]
    }
  ]
}

完成这些步骤后,您可以在调试模式下按 F11 后看到真正的异步方法.

After these steps you can see your real async method after pressing F11 in debug mode.