Node.JS官方文檔:https://nodejs.dev/en/
創(chuàng)建異步函數(shù),并返回相關(guān)數(shù)值:
一般方式創(chuàng)建
/*
函數(shù)直接通過返回Promise成為異步函數(shù)
異步函數(shù):返回promise的函數(shù)稱之為異步函數(shù)
*/
function fn(){
return Promise.resolve(10)
}
// 讀取結(jié)果需要通過then去讀取
fn().then(r => {
console.log(r)
})
通過async方式創(chuàng)建:
/*
通過async可以快速的創(chuàng)建異步函數(shù)
*/
/*
通過async可以來創(chuàng)建一個(gè)異步函數(shù),fn2() 此時(shí)就是一個(gè)異步函數(shù)
異步函數(shù)的返回值回自動(dòng)封裝到一個(gè)Promise中返回
*/
async function fn2(){
return "async返回的數(shù)據(jù)10"
}
// 讀取結(jié)果需要通過then去讀取
fn2().then(r => {
console.log(r)
})
在async聲明的函數(shù)中可以使用await關(guān)鍵字來調(diào)用異步函數(shù)
/*
在async聲明的異步函數(shù)中可以使用await關(guān)鍵字來調(diào)用異步函數(shù)
*/
// 創(chuàng)建一個(gè)函數(shù)計(jì)算 a + b的結(jié)果,但是異步,即返回的Promise
function sum(a, b){
return new Promise(resolve => {
setTimeout(() => {
resolve(a + b)
}, 2000)
})
}
// 通過async創(chuàng)建一個(gè)異步函數(shù)
async function fn3() {
sum(123, 456).then(r => {
console.log(r)
})
}
// 調(diào)用fn3()
fn3()
- 當(dāng)我們通過await去調(diào)用異步函數(shù)時(shí)候,它會(huì)暫停代碼的運(yùn)行
- 直到異步代碼執(zhí)行有結(jié)果時(shí),才會(huì)將結(jié)果返回
- 注意 await只能用于 async聲明的異步函數(shù)中,或者es模塊的緊急作用域中
- await阻塞的只是異步函數(shù)內(nèi)部的代碼,不會(huì)影響外部代碼
- 通過 await 調(diào)用異步代碼時(shí),需要通過try-catch來處理異常
/*
在async聲明的異步函數(shù)中可以使用await關(guān)鍵字來調(diào)用異步函數(shù)
*/
// 創(chuàng)建一個(gè)函數(shù)計(jì)算 a + b的結(jié)果,但是異步,即返回的Promise
function sum1(a, b){
return new Promise(resolve => {
setTimeout(() => {
resolve(a + b)
}, 2000)
})
}
/*
Promise解決了異步調(diào)用中回調(diào)函數(shù)問題
雖然通過鏈?zhǔn)秸{(diào)用解決了回調(diào)地獄,但是鏈?zhǔn)秸{(diào)用太多以后還是不好看
但現(xiàn)在要求以同步的方式去調(diào)用異步的代碼
*/
async function fn4() {
// 鏈?zhǔn)秸{(diào)用
// sum1(123, 456)
// .then(r => sum(r, 8))
// .then(r => sum(r, 8))
// .then(r => console.log(r))
// 當(dāng)我們通過await去調(diào)用異步函數(shù)時(shí)候,它會(huì)暫停代碼的運(yùn)行
// 直到異步代碼執(zhí)行有結(jié)果時(shí),才會(huì)將結(jié)果返回
// 注意 await只能用于 async聲明的異步函數(shù)中,或者es模塊的緊急作用域中
// await阻塞的只是異步函數(shù)內(nèi)部的代碼,不會(huì)影響外部代碼
// 通過 await 調(diào)用異步代碼時(shí),需要通過try-catch來處理異常
try{
let result = await sum(123, 456)
result = await sum(result, 8)
result = await sum(result, 9)
console.log(result)
}catch(e){
console.log("出錯(cuò)了")
}
// awwit阻塞的是異步函數(shù)內(nèi)部的代碼
// console.log(123)
// console.log(222)
// console.log(333)
}
// 調(diào)用fn3()
fn4()
// await不會(huì)阻塞外部代碼
console.log("外部代碼")
如果async聲明的函數(shù)沒有寫await,那么它就會(huì)依次執(zhí)行
// 如果async聲明的函數(shù)中沒有寫await,那么它里面就會(huì)依次執(zhí)行
async function fn4(){
console.log(1)
console.log(2)
console.log(3)
console.log(4)
// 如果有return
return 10
}
fn4()
// fn4等價(jià)于fn5
function fn5() {
return new Promise(resolve => {
console.log(1)
console.log(2)
console.log(3)
console.log(4)
resolve(10) // return放在resolve中 fn4如果沒有返回值,resolve就為空
})
}
fn5()
console.log(5) // 執(zhí)行結(jié)果 1 2 3 4 5 1 2 3 4 5 6
使用await調(diào)用函數(shù)后,await當(dāng)前函數(shù)后的所有代碼,會(huì)先進(jìn)入微任務(wù)隊(duì)列文章來源:http://www.zghlxwxcb.cn/news/detail-432310.html
await后的所有代碼,都會(huì)放入到微任務(wù)隊(duì)列中執(zhí)行文章來源地址http://www.zghlxwxcb.cn/news/detail-432310.html
// 同步代碼前加await
async function fn6(){
console.log(111)
/*
當(dāng)我們使用await調(diào)用函數(shù)后,await當(dāng)前函數(shù)后的所有代碼
會(huì)在await當(dāng)前函數(shù)執(zhí)行完畢后,被列入微任務(wù)隊(duì)列中
*/
await console.log(112)
// await后的所有代碼,都會(huì)放入到微任務(wù)隊(duì)列中執(zhí)行
console.log(113)
}
fn6()
console.log(222) // 執(zhí)行結(jié)果為 111 112 222 113
// 等價(jià)于
function fn7() {
return new Promise(resolve => {
console.log(111)
// 上面的在此處加了await
console.log(112)
resolve()
}).then(r => {
console.log(113)
})
}
到了這里,關(guān)于async和await的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!