Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of await and async in es6

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about the role of await and async in es6. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

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

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

What is the function of await and async in es6

Async is used to declare that an function is asynchronous, while await is used to wait for the execution of an asynchronous method to complete.

Await can only be placed in the async function

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

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

If you are waiting for promise, wait for Promise to complete, and then take the parameter of resolve as the result of the await expression.

If you are waiting for an expression, execute the async external synchronization code and then come back to continue execution.

Async

Async functions (including function statements, function expressions, Lambda expressions) return a Promise object.

If you return a direct quantity in a function, async wraps the direct quantity into a Promise object with Promise.resolve ().

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

The characteristic of Promise is that there is no wait, so if you execute the 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 thought that await is waiting for an async function to complete. However, according to the syntax, await is waiting for an expression that evaluates to a Promise object or other value (in other words, without special qualification).

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 promise (the value here).

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

If it does not wait for a Promise object, then the await expression evaluates to what it waits for.

Why use await?

In order to make our asynchronous code more like synchronous code

There are multiple promise. How to get the result after all the promise are finished?

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

These are all the contents of the article "what are the functions of await and async in es6". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report