In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "what is the difference between promise,async and await in Javascript", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the difference between promise,async and await in Javascript" can help you solve your doubts.
Finally figure out the difference and connection between promise, async and await. Take a look at the following code
Promise 2 is the way to write it.
6 is the way to write async and await.
Mainly look at the second writing method and the sixth writing method, the second writing method is the typical writing method of promise, and the sixth writing method is the typical writing method of async and await.
/ / the following three requests execute req1 = () > {return fetch ("http://example.com/api/v1/get")}req2 = () = > {return fetch (" http://example.com/api/v1/post")}req3 = () = > {return fetch ("1req1 () .then (res= > {console.log (" 1: ")) Res) req2 () .then (res = > {console.log ("2:", res) req3 () .then (res = > {console.log ("3:", res)})}) / / write 2req1 () .then (res = > {console.log ("1:", res) return req2 ()}) .then (res = > {console.log ("2:") Res) return req3 ()}) .then (res = > {console.log ("3:", res)}) / / 3function f1 () {req1 () req2 () req3 ()} / / 4async function f2 () {await req1 await req2 await req3} / / 5async function f3 () {req1 () .then (res = > {console.log ("1:") Res)}) await f3 () 1 ()} async function f3 () 1 () {req1 (). Then (res= > {console.log ("2:", res)}) await f3 (2) ()} async function f3 () 2 () {req2 () .then (res= > {console.log ("3:") Res)} / / write 6ff () async function ff () {await req1_good ()} async function req1_good () {fetch ("http://example.com/api/v1/get").then(res = > {console.log (" 1: ") Res) await req2_good ()} async function req2_good () {fetch ("http://example.com/api/v1/post").then(res = > {console.log (" 2: ", res)}) await req3_good ()} async function req3_good () {fetch (" http://example.com/api/v1/delete").then(res = > {console.log ("3:") Res)})}
The outermost async function is returned immediately after the call, but the async is still executed layer by layer.
The function of await is to wait for all await functions inside the modified function to be executed.
Starting an async function from the outermost layer is equivalent to a go program, and await func is also equivalent to a go program, the difference is that await = go + waitgroup
What makes await smarter than promise is that when promise calls another promise in then, it has to return another promise to then, or call back in then, but await does not need it at all.
Async is for async, await is for async + blocking, and none of them is indispensable.
After reading this, the article "what's the difference between promise,async and await in Javascript" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.