Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the VS Code "Debug Console", run a JavaScript await function

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?

like image 659
Gary Avatar asked May 26 '18 21:05

Gary


People also ask

How do I run JavaScript in Visual Studio Code console?

Open the JavaScript code file in Text Editor, then use shortcut Control + Alt + N (or ⌃ Control + ⌥ Option + N on macOS), or press F1 and then select/type Run Code , the code will run and the output will be shown in the Output Window.

What is JavaScript Debug terminal in VS Code?

JavaScript Debug Terminal# In a similar way to auto attach, the JavaScript Debug Terminal will automatically debug any Node. js process you run in it. You can create a Debug Terminal by running the Debug: Create JavaScript Debug Terminal command from the Command Palette ( kbs(workbench. action.

What is console Debug in JavaScript?

The console. debug() method outputs a message to the web console at the "debug" log level. The message is only displayed to the user if the console is configured to display debug output. In most cases, the log level is configured within the console UI.


1 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.

like image 190
Andrej K Avatar answered Oct 23 '22 08:10

Andrej K