Answer a question

In the VS Code "Debug Console", I can evaluate expressions on my code in the middle of a debugging session when debugging my JavaScript code, including running my functions. However, I seem to be unable to run async functions, even when I use an IIFE, etc.

I have the following code:

const axios = require('axios');

async function getUrl() {
  const response = await axios.get('http://example.com/');
  return response.data;
}

async function main() {
  const response = await getUrl();
  console.log(response);
}

main();

I set a breakpoint on the async function main() { line, then I run the VS Code Debugger. I then try to run the getUrl function in different ways in the Debug Console, such as getUrl(), getUrl().then((data) => data), and (async () => getUrl())(), which all return Promise { pending }). main() returns the same (if run after the function's definition). None of these methods print the function's return value.

I also tried await getUrl(), which returns SyntaxError: await is only valid in async function.

I know I can output the functions I want, within the code with console.log, if really necessary, but I'm looking for a solution that specifically uses the Debug Console to print the results of promises specifically created with an await function.

Therefore, is there really any way to output the results of a function in the VS Code Debug Console, when the function is async?

Answers

VS Code Debug Console supports top level async/await (https://github.com/microsoft/vscode-js-debug#top-level-await) however the issue might be you're paused on the breakpoint.

if you use await while paused on a breakpoint, you'll only get a pending Promise back. This is because the JavaScript event loop is paused while on a breakpoint.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐