async/await實(shí)現(xiàn)Promise.acll()簡(jiǎn)介
一、Promise.all()簡(jiǎn)介
Promise.all() 方法接收一個(gè) promise 的 iterable 類型(注:Array,Map,Set 都屬于 ES6 的 iterable 類型)的輸入,并且只返回一個(gè)Promise實(shí)例,并且輸入的所有 promise 的 resolve 回調(diào)的結(jié)果是一個(gè)數(shù)組。
- Promise的 resolve 回調(diào)執(zhí)行是在所有輸入的 promise 的 resolve 回調(diào)都結(jié)束,或者輸入的 iterable 里沒(méi)有 promise 了的時(shí)候
- Promise的 reject 回調(diào)執(zhí)行是只要任何一個(gè)輸入的 promise 的 reject 回調(diào)執(zhí)行或者輸入不合法的 promise 就會(huì)立即拋出錯(cuò)誤,并且只要有 reject 就會(huì)立即拋出的錯(cuò)誤信息。
二、async/await實(shí)現(xiàn)Promise.acll()
先定義三個(gè)Promise實(shí)例對(duì)象,并放置于一個(gè)數(shù)組中
let a = new Promise((res, rej) => { res(1) }).catch(err => console.log(err)) let b = new Promise((res, rej) => { setTimeout(() => { rej(2) }, 2000) }).catch(err => console.log(err)) let c = new Promise((res, rej) => { res(3) }).catch(err => console.log(err)) const arr = [a, b, c]
1、方式一
-使用async/await,循環(huán)遍歷Promise實(shí)例對(duì)象的數(shù)組,并把每個(gè)Promise對(duì)象的結(jié)果放置于一個(gè)空數(shù)組中。
async function bb() { let arr1 = []; try { for (let i = 0; i < arr.length; i++) { let h = await arr[i] arr1.push(h) } } catch (err) { } return arr1 } bb().then(res => { console.log(res); //[1, undefined, 3] });
undefined
是因?yàn)閍wait只處理成功時(shí)resolve(),不處理失敗異常,故返回undefined
2、方式二
該方面類似實(shí)現(xiàn)手寫(xiě)Promise.acll(),等await拿到Promise結(jié)果然后count加1,知道count的數(shù)值等于數(shù)值的長(zhǎng)度在resolve()
const all = (arr) => { return new Promise((resolve, reject) => { let length = arr && arr.length let count = 0 let result = [] if (!arr || arr.length === 0) { resolve(result) } arr.forEach(async (item, index) => { try { const res = await item result[index] = res count++ if (count === length ) { resolve(result) } } catch (err) { reject(err) } }); }) }
三、async/await與Promise.acll()結(jié)合使用
因?yàn)镻romise.all()返回的也是Promise,所以await 后面可以跟Promise.all()
function fn() { return new Promise((resolve, reject) => { resolve(Math.random()) }) } async function asyncFunc() { let result try { result = await Promise.all([fn(), fn()]) console.log(result) } catch (err) { console.log(err, 'err') } return result } asyncFunc().then(res => { console.log(res, 'res') })
到此這篇關(guān)于async/await實(shí)現(xiàn)Promise.acll()簡(jiǎn)介的文章就介紹到這了,更多相關(guān)async/await實(shí)現(xiàn)Promise.acll()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript中利用柯里化函數(shù)實(shí)現(xiàn)bind方法
這篇文章主要為大家詳細(xì)介紹了javascript中利用柯里化函數(shù)實(shí)現(xiàn)bind方法,感興趣的小伙伴們可以參考一下2016-04-04通過(guò)flv.js播放監(jiān)控示例深入探究直播流技術(shù)
本文記錄一下在使用 flv.js 播放監(jiān)控視頻時(shí)踩過(guò)的各種各樣的坑,雖然官網(wǎng)給的?Getting Started?只有短短幾行代碼,跑一個(gè)能播視頻的 demo 很容易,但是播放時(shí)各種各樣的異常會(huì)搞到你懷疑人生,下面我將自己踩過(guò)的坑,以及踩坑過(guò)程中補(bǔ)充的相關(guān)知識(shí),詳細(xì)總結(jié)一下2023-10-10JavaScript學(xué)習(xí)筆記之?dāng)?shù)組去重
這篇文章主要介紹了JavaScript學(xué)習(xí)筆記之?dāng)?shù)組去重的相關(guān)資料,需要的朋友可以參考下2016-03-03微信小程序?qū)崙?zhàn)之網(wǎng)易云音樂(lè)歌曲詳情頁(yè)實(shí)現(xiàn)代碼
本文給大家介紹了微信小程序?qū)W習(xí)記錄之網(wǎng)易云音樂(lè)歌曲詳情頁(yè)代碼實(shí)現(xiàn),代碼分為html、css和js部分,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05jqgrid 表格數(shù)據(jù)導(dǎo)出實(shí)例
jqgrid并沒(méi)有自帶導(dǎo)出表格數(shù)據(jù)的方法,這里就自己實(shí)現(xiàn)了一個(gè),嘗試過(guò)在頁(yè)面直接將數(shù)據(jù)導(dǎo)出,發(fā)現(xiàn)只有IE下可以通過(guò)調(diào)用saveas來(lái)實(shí)現(xiàn),但是別的瀏覽器不支持,于是考慮將數(shù)據(jù)傳回后臺(tái),然后后臺(tái)返回下載文件來(lái)實(shí)現(xiàn)2013-11-11js實(shí)現(xiàn)倒計(jì)時(shí)時(shí)鐘的示例代碼
本篇文章主要是對(duì)js倒計(jì)時(shí)時(shí)鐘的示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12JS字符串去除連續(xù)或全部重復(fù)字符的實(shí)例
這篇文章主要介紹了JS字符串去除連續(xù)或全部重復(fù)字符的實(shí)例,需要的朋友可以參考下2018-03-03