In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 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.
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.