undefinedfix
Sign in

How does async function embody asynchrony?

haydenseitz edited in Fri, 18 Nov 2022

How does async function embody asynchrony?

2 Replies
ePumps
commented on Fri, 18 Nov 2022
async function xxx () {
  // ...
  //异步代码
  await xxx;
  
  // 异步代码执行后才执行
  // ...
}

It is to write asynchronous code synchronously. The code after await will wait until the asynchronous code is executed.

mohaa8844
commented on Fri, 18 Nov 2022

It doesn't show much. Async is just a syntax sugar. Async function is just a common function that returns promise object, but await can be used in the function. If await is not used, async function is no different from ordinary function that returns promise object. Even if await is used, async function is executed according to a common return promise object function.

Await has the effect of pseudo synchronization. Await is followed by a promise object. Await makes people feel that it is to wait for the promise object to resolve before executing the code behind await. But in fact, it only calls the then method of promise object, and then takes the code behind await as the parameter of then. The async function does not really wait for the promise object after await to resolve to return. It returns immediately.