JS循環(huán)中正確使用async、await的姿勢(shì)分享
概覽(循環(huán)方式 - 常用)
- for
- map
- forEach
- filter
聲明遍歷的數(shù)組和異步方法
聲明一個(gè)數(shù)組:??
const skills = ['js', 'vue', 'node', 'react']
再聲明一個(gè)promise的異步代碼: ??
function getSkillPromise (value) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(value) }, 1000) }) }
for 循環(huán)中使用
由于for循環(huán)并非函數(shù),而async、await需要在函數(shù)中使用,因此需要在for循環(huán)外套一層function
async function test () { for (let i = 0; i < skills.length; i++) { const skill = skills[i] const res = await getSkillPromise(skill) console.log(res) } } test() // 調(diào)用
當(dāng)使用await時(shí),希望JavaScript暫停執(zhí)行,直到等待 promise 返回處理結(jié)果。上述結(jié)果意味著for循環(huán)中有異步代碼,是可以等到for循環(huán)中異步代碼完全跑完之后再執(zhí)行for循環(huán)后面的代碼。
但是他不能處理回調(diào)的循環(huán),如forEach、map、filter等,下面具體分析。
map 中使用
在map中使用await, map 的返回值始是promise數(shù)組,這是因?yàn)楫惒胶瘮?shù)總是返回promise。
async function test () { console.log('start') const res = skills.map(async item => { return await getSkillPromise(item) }) console.log(res) console.log('end') } test()
結(jié)果:始終為promise數(shù)組
start [ Promise { <pending> }, Promise { <pending> }, Promise { <pending> }, Promise { <pending> } ] end
若果你想要等到promise的返回結(jié)果,可以使用promise.all()處理一下
async function test () { console.log('start') const res = skills.map(async item => { return await getSkillPromise(item) }) const resPromise = await Promise.all(res) console.log(resPromise) console.log('end') } test() // 結(jié)果 start [ 'js', 'vue', 'node', 'react' ] end
forEach 中使用
先上代碼和結(jié)果
async function test () { console.log('start') skills.forEach(async item => { const res = await getSkillPromise(item) console.log(res) }) console.log('end') } test()
預(yù)期結(jié)果
'Start'
'js'
'vue'
'node'
'react'
'End'
實(shí)際結(jié)果 在forEach循環(huán)等待異步結(jié)果返回之前就執(zhí)行了console.log('end')
'Start'
'End'
'js'
'vue'
'node'
'react'
JavaScript 中的 forEach不支持 promise 感知,也支持 async 和await,所以不能在 forEach 使用 await 。
filter 中使用
使用filter過(guò)濾item為vue或者react的選項(xiàng)
正常使用 filter:
async function test () { console.log('start') const res = skills.filter(item => { return ['vue', 'react'].includes(item) }) console.log(res) console.log('end') } test() // 調(diào)用 // 結(jié)果 start [ 'vue', 'react' ] end
使用 await后:
async function test () { console.log('start') const res = skills.filter(async item => { const skill = await getSkillPromise(item) return ['vue', 'react'].includes(item) }) console.log(res) console.log('end') } test()
預(yù)期結(jié)果:
start
[ 'vue', 'react' ]
end
實(shí)際結(jié)果:
[ 'js', 'vue', 'node', 'react' ]
end
結(jié)論:因?yàn)楫惒胶瘮?shù)getSkillPromise返回結(jié)果返回的promise總是真的,所以所有選項(xiàng)都通過(guò)了過(guò)濾
附使用小結(jié)
- 如果你想連續(xù)執(zhí)行await調(diào)用,請(qǐng)使用for循環(huán)(或任何沒(méi)有回調(diào)的循環(huán))。
- 永遠(yuǎn)不要和forEach一起使用await,而是使用for循環(huán)(或任何沒(méi)有回調(diào)的循環(huán))。
- 不要在 filter 和 reduce 中使用 await,如果需要,先用 map 進(jìn)一步驟處理,然后在使用 filter 和 reduce 進(jìn)行處理。
結(jié)語(yǔ):由于工作中遇到了超大表單抽離組件,異步校驗(yàn)是遇到了此問(wèn)題,后來(lái)經(jīng)過(guò)查閱資料總結(jié)出來(lái)的結(jié)果
總結(jié)
到此這篇關(guān)于JS循環(huán)中正確使用async、await的文章就介紹到這了,更多相關(guān)JS循環(huán)中使用async、await內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
layer 刷新某個(gè)頁(yè)面的實(shí)現(xiàn)方法
今天小編就為大家分享一篇layer 刷新某個(gè)頁(yè)面的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09ajaxControlToolkit AutoCompleteExtender的用法
昨天在搜索中使用了這個(gè)控件,不過(guò)不知道為什么在IE中反應(yīng)比較慢2008-10-10JS實(shí)現(xiàn)讀取xml內(nèi)容并輸出到div中的方法示例
這篇文章主要介紹了JS實(shí)現(xiàn)讀取xml內(nèi)容并輸出到div中的方法,涉及javascript針對(duì)xml格式數(shù)據(jù)的讀取、遍歷、輸出等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04JavaScript實(shí)現(xiàn)計(jì)算多維嵌套數(shù)組深度
在前端開(kāi)發(fā)中,經(jīng)常會(huì)遇到需要處理多維嵌套的數(shù)據(jù)結(jié)構(gòu),并需要計(jì)算出它們的深度,本文就來(lái)講講如何使用JavaScript實(shí)現(xiàn)計(jì)算多維嵌套數(shù)組深度吧2023-06-06解讀Bootstrap v4 sass設(shè)計(jì)
這篇文章主要介紹了Bootstrap v4 sass設(shè)計(jì)的相關(guān)資料,需要的朋友可以參考下2016-05-05TypeScript高級(jí)用法的知識(shí)點(diǎn)匯總
這篇文章主要給大家介紹了關(guān)于TypeScript高級(jí)用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用TypeScript具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12