In the VS Code "Debug Console", run a JavaScript await function
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.
更多推荐
所有评论(0)