如何在VSCode调试器中执行异步功能? [英] How to execute async functions in VSCode debugger?

查看:144
本文介绍了如何在VSCode调试器中执行异步功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用一些JavaScript代码进入VSCode调试器,并在等待时调用异步函数,它将返回一个Promise.如何在调试器中解决诺言,以便查看结果是什么?

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?

这不是>如何调试异步/等待Visual Studio代码吗?

这个问题似乎是在询问如何在调试器中实际执行该函数的同时在异步函数中放置断点.

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.

推荐答案

您可以这样做:

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

然后在VSCode中运行调试器.

And then run debugger in VSCode.

这篇关于如何在VSCode调试器中执行异步功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆