简体   繁体   English

异步等待不适用于“then”和“catch”

[英]Async await not working with 'then' and 'catch'

There are two scenarios in my case.在我的情况下有两种情况。

代码

In the first case the code is working fine.在第一种情况下,代码工作正常。

Why is it throwing an error in this case.为什么在这种情况下会抛出错误。 Can't I use await in then or is there a problem with using try catch inside the then statement?我不能在then使用 await 还是在then语句中使用 try catch 有问题?

代码

I should also mention that the platform that I am coding in is a little different but it accepts pure JavaScript too.我还应该提到,我正在编码的平台有点不同,但它也接受纯 JavaScript。

Await will already resolve your promise, so "then" wont be having any promise to resolve. Await 已经解决了你的承诺,所以“then”不会有任何承诺解决。

For await async try this对于等待异步试试这个

    private myMethod = async () => {
   try {
      const response = await yourPromise();
   } catch (e) {
      console.error(e)
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM