简体   繁体   中英

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?

代码

I should also mention that the platform that I am coding in is a little different but it accepts pure JavaScript too.

Await will already resolve your promise, so "then" wont be having any promise to resolve.

For await async try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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