In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "JavaScript ES6 asynchronous functions what", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "JavaScript ES6 asynchronous functions have what"!
Asynchronous functionality consists of two parts:
1) Regular async functions prefixed with function
1
2
3
4
async function fetchdata(url){
// Do something
// Always returns a promise
}
2) await uses keywords before asynchronous function calls in the main Async function.
One example is worth a thousand words. The following is a rewrite of the Promise-based example to use the Async function:
1
2
3
4
five
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function getasync(url) { // same as original function
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open("GET", url)
xhr.onload = () => resolve(xhr.responseText)
xhr.onerror = () => reject(xhr.statusText)
xhr.send()
})
}
async function fetchdata(){ // main Async function
var text1 = await getasync('test.txt')
console.log(text1)
var text2 = await getasync('test2.txt')
console.log(text2)
var text3 = await getasync('test3.txt')
console.log(text3)
return "Finished"
}
fetchdata().then((msg) =>{
console.log(msg) // logs "finished"
})
The above example runs with "test.txt","test2.txt","test3.txt", and finally "done" in that order.
As you can see, inside asynchronous functions, we refer to the asynchronous function getasync() as a synchronous function-there is no need for then() methods or callback functions to indicate the next step. Whenever await encounters a keyword, execution pauses until getasync() resolves, and then moves to the next line in the Async function. The result is the same as a purely Promised based approach, using a series of then() methods.
At this point, I believe that everyone has a deeper understanding of "JavaScript ES6 asynchronous functions", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.