How to execute async functions in VSCode debugger? [duplicate]

夙愿已清 提交于 2020-12-15 06:50:16

问题


If I drop into the VSCode debugger in some javascript code and call an asynchronous function with await it just returns a promise. How can I resolve the promise within the debugger so I can see what the result is?

For example, if I define a function like so:

const doAsyncThing = async () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(5)
        }, 1000)
    })
}

Then this happens when I call it in the debugger:

> const result = await doAsyncThing()
Promise {<pending>}
> result
Uncaught ReferenceError: result is not defined
> doAsyncThing().then(r => console.log(r))
Promise {<pending>}
> await doAsyncThing().then(r => console.log(r))
Promise {<pending>}

How can I make it resolve the promise and get me the result within the debugger?

This is not a duplicate of How to debug async/await in visual studio code?

That question appears to be asking how to place a breakpoint within an asynchronous function, while I am trying to actually execute the function from the debugger.

I recreated this question in a new post here since I don't believe it is a duplicate of the above post. This is my first time posting so hopefully that's the right thing to do.


回答1:


Maybe put an "await" in front of the function call?




回答2:


You can do it like this:

const result = await Promise.resolve("success");
debugger

And then run debugger in VSCode.



来源:https://stackoverflow.com/questions/64996504/how-to-execute-async-functions-in-vscode-debugger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!