async and await with promises


async

async is used to declare an asynchronous function

return returns the promise object

In  await the absence of an async function, it executes immediately, returns a Promise object, and never blocks subsequent statements. This is no different from a normal function returning a Promise object.

two await

await waits for an expression that evaluates to a Promise object or other value

  If it's not a Promise, the await expression evaluates to what it's waiting for.

  If it waits for a Promise object, await will block the following code, wait for the Promise object to resolve, and then get the resolved value as the operation result of the await expression.

This process of awiat waiting turns the code into near-synchronous

In fact, this is why await must be used in async functions. The async function call does not cause blocking, and all internal blocking is encapsulated in a Promise object for asynchronous execution.

Guess you like

Origin blog.csdn.net/wanjun_007/article/details/126944295