Home  >  Article  >  Web Front-end  >  What are the functions of await and async in es6?

What are the functions of await and async in es6?

WBOY
WBOYOriginal
2022-05-06 17:25:002673browse

The functions of await and async in es6 are: 1. async is used to declare that a function is executed asynchronously and returns a Promise object; 2. await is used to wait for the completion of an asynchronous method, because await can only Used in async functions, so it is used to wait for the return value of an async function.

What are the functions of await and async in es6?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What are the functions of await and async in es6

async is used to declare that a function is asynchronous, while await is used to wait for an asynchronous method to complete execution.

await can only be placed in async functions

Await is followed by a function that returns a new promise and executes it.

When encountering await, the following code will be blocked, and the synchronization code outside async will be executed first.

If you are waiting for a promise, wait for the promise to complete, and then express the resolve parameters as await. The result of the operation of the formula.

If you are waiting for an expression, after executing the synchronization code outside async, come back and continue executing

async

async function (including functions Statement, function expression, Lambda expression) will return a Promise object.

If you return a direct value in a function, async will encapsulate the direct value into a Promise object through Promise.resolve().

What if the async function does not return a value? It's easy to imagine that it returns Promise.resolve(undefined).

Characteristics of Promise - no waiting, so if you execute an async function without await, it will execute immediately, return a Promise object, and will never block subsequent statements. This is no different from a normal function that returns a Promise object.

await

Generally speaking, it is believed that await is waiting for an async function to complete. However, according to the syntax, await is waiting for an expression, and the evaluation result of this expression is a Promise object or other value (in other words, there are no special restrictions).

Because the async function returns a Promise object, await can be used to wait for the return value of an async function, that is, the resolve of the promise (the value here).

await is not only used to wait for Promise objects, it can wait for the result of any expression. Therefore, await can actually be followed by ordinary function calls or direct quantities.

If what it waits for is not a Promise object, the result of the await expression is what it waits for.

Why use await

In order to make our asynchronous code more like synchronous code

There are multiple promises, how to get the result after all promises have ended

Because Promise.all() also returns a promise, if you want to use await to get the values ​​of multiple promises, you can directly await Promise.all()

[Related recommendations: javascript video tutorialwebfrontend

The above is the detailed content of What are the functions of await and async in es6?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn