使用Node.js的async和await進(jìn)行異步編程
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ì)列
await后的所有代碼,都會(huì)放入到微任務(wù)隊(duì)列中執(zhí)行
// 同步代碼前加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)于使用Node.js的async和await進(jìn)行異步編程的文章就介紹到這了,更多相關(guān)Node.js async和await內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nodejs npm install全局安裝和本地安裝的區(qū)別
這篇文章主要介紹了nodejs npm install 全局安裝和非全局安裝的區(qū)別,即帶參數(shù)-g和不帶參數(shù)-g安裝的區(qū)別,需要的朋友可以參考下2014-06-06nodejs入門教程二:創(chuàng)建一個(gè)簡(jiǎn)單應(yīng)用示例
這篇文章主要介紹了nodejs入門教程之創(chuàng)建一個(gè)簡(jiǎn)單應(yīng)用的方法,涉及nodejs http模塊的引用、端口監(jiān)聽等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04nodejs版本過高導(dǎo)致vue2版本的項(xiàng)目無法正常啟動(dòng)的解決方案
這篇文章主要給大家介紹了關(guān)于nodejs版本過高導(dǎo)致vue2版本的項(xiàng)目無法正常啟動(dòng)的解決方案,本文小編給大家詳細(xì)介紹了如何解決這個(gè)問題,如有遇到同樣問題的朋友可以參考下2023-11-11nodejs async異步常用函數(shù)總結(jié)(推薦)
這篇文章主要介紹了nodejs async異步常用函數(shù)總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-11-11