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

The synchronous execution method of JavaScript Asynchronous Operation

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

Share

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

In this article, the editor introduces in detail "the method of synchronous execution of JavaScript asynchronous operation", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "the method of synchronous execution of JavaScript asynchronous operation" can help you solve your doubts.

Let's look at the simplest example. My requirement is to print msg outside the fn1 function.

Function fn1 () {

SetTimeout (function () {

Msg = 'give me a minute. I'll give you 3s'.

}, 3000)

}

Fn1 ()

So how can I get msg in the fn function?

Through the callback function

Function fn1 (callBack) {

SetTimeout (function () {

Msg = 'give me a minute. I'll give you 3s'.

CallBack (msg); / / callback

}, 3000)

}

Fn1 (function (c) {

Alert (c)

})

Es6

Fn1 (c = > {

Alert (c)

})

Promise changes the asynchronous request to be called by promise through .then ()

Function fn1 () {

Return new Promise ((resolve, reject) = > {

SetTimeout () = > {

Msg = 'give me a minute. I'll give you 3s'.

Resolve (msg)

}, 3000)

})

}

/ / call through .then ()

Fn1 () .then (res = > {

Alert (res)

})

Handled by await and async

Function fn1 () {

Return new Promise ((resolve, reject) = > {

SetTimeout () = > {

Msg = 'give me a minute. I'll give you 3s'.

Resolve (msg)

}, 3000)

})

}

Async function passpro () {

Let res = await fn1 ()

Alert (res)

}

Passpro ()

The async function returns a Promise object

If a function adds the async keyword, the function has a return value

When calling this function, if the function executes successfully

The Promise.solve () method is called internally to return a Promise object.

If an exception occurs in the execution of the function

The Promise.reject () method is called to return a promise object

After reading this, the article "the method of synchronous execution of JavaScript Asynchronous Operation" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow 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