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

How to use the js async function

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use the js async function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the js async function.

The async function is the syntax sugar of the Generator function. The async function simply replaces the asterisk (*) of the Generator function with async and yield with await, that's all.

The improvement point of async function to Generator function

The built-in executor does not need to call next () like the Generator function to make the function execute.

Better semantics

The promise object is returned

The async function returns a promise object

The async function returns a Promise object. The value returned by the return statement inside the async function becomes the parameter of the callback function of the then method. An error is thrown inside the async function, which causes the returned Promise object to become reject. The error object thrown will be received by the callback function of the catch method

See the following example:

LettestAsync=asyncfunction () {

/ / thrownewError ('throw an error ~')

Reject ()

}

TestAsync () .then (resolve= > {

Console.log (`resolve:$ {resolve} `)

}, reject= > {

Console.log (`reject:$ {reject} `); / / the callback function in which the status changes to reject will be executed. If there is no callback function here, catch will capture it. This is the thing of promise.

}) .catch (err= > {

Console.log (`err:$ {err} `)

})

If any Promise object following an await statement changes to the reject state, the entire async function interrupts execution.

Asyncfunctionf () {

AwaitPromise.reject ('error')

AwaitPromise.resolve ('helloworld'); / / will not be executed

}

/ / the promise statement on the second line will not be executed

At this point, I believe you have a deeper understanding of "how to use the js async function". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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