這裏指的遍歷方法包括:
map
、reduce
、reduceRight
、forEach
、filter
、some
、every
由於最近要進行了一些數據彙總,node
版本已是8.11.1了,因此直接寫了個async/await
的腳本。
可是在對數組進行一些遍歷操做時,發現有些遍歷方法對Promise
的反饋並非咱們想要的結果。javascript
固然,有些嚴格來說並不能算是遍歷,好比說some
,every
這些的。
但確實,這些都會根據咱們數組的元素來進行屢次的調用傳入的回調。java
這些方法都是比較常見的,可是當你的回調函數是一個Promise
時,一切都變了。node
async/await
爲Promise
的語法糖 文中會直接使用async/await
替換Promise
git
let result = await func()
// => 等價於
func().then(result => {
// code here
})
// ======
async function func () {
return 1
}
// => 等價與
function func () {
return new Promise(resolve => resolve(1))
}
複製代碼
map
能夠說是對Promise
最友好的一個函數了。
咱們都知道,map
接收兩個參數:github
this
指向的參數[1, 2, 3].map(item => item ** 2) // 對數組元素進行求平方
// > [1, 4, 9]
複製代碼
上邊是一個普通的map
執行,可是當咱們的一些計算操做變爲異步的:數組
[1, 2, 3].map(async item => item ** 2) // 對數組元素進行求平方
// > [Promise, Promise, Promise]
複製代碼
這時候,咱們獲取到的返回值其實就是一個由Promise
函數組成的數組了。異步
因此爲何上邊說map
函數爲最友好的,由於咱們知道,Promise
有一個函數爲Promise.all
會將一個由Promise
組成的數組依次執行,並返回一個Promise
對象,該對象的結果爲數組產生的結果集。async
await Promise.all([1, 2, 3].map(async item => item ** 2))
// > [1, 4, 9]
複製代碼
首先使用Promise.all
對數組進行包裝,而後用await
獲取結果。函數
reduce
的函數簽名想必你們也很熟悉了,接收兩個參數:性能
accumulator
累加的值currentValue
當前正在處理的元素currentIndex
當前正在處理的元素下標 (感謝@NeurotoxinVX小夥伴的補充)array
調用reduce
的數組accumulator
的初始值[1, 2, 3].reduce((accumulator, item) => accumulator + item, 0) // 進行加和
// > 6
複製代碼
這個代碼也是沒毛病的,一樣若是咱們加和的操做也是個異步的:
[1, 2, 3].reduce(async (accumulator, item) => accumulator + item, 0) // 進行加和
// > Promise {<resolved>: "[object Promise]3"}
複製代碼
這個結果返回的就會很詭異了,咱們在回看上邊的reduce
的函數簽名
對每一項元素執行的回調函數,返回值將被累加到下次函數調用中
而後咱們再來看代碼,async (accumulator, item) => accumulator += item
這個在最開始也提到了,是Pormise
的語法糖,爲了看得更清晰,咱們能夠這樣寫:
(accumulator, item) => new Promise(resolve =>
resolve(accumulator += item)
)
複製代碼
也就是說,咱們reduce
的回調函數返回值其實就是一個Promise
對象
而後咱們對Promise
對象進行+=
操做,獲得那樣怪異的返回值也就很合情合理了。
固然,reduce
的調整也是很輕鬆的:
await [1, 2, 3].reduce(async (accumulator, item) => await accumulator + item, 0)
// > 6
複製代碼
咱們對accumulator
調用await
,而後再與當前item
進行加和,在最後咱們的reduce
返回值也必定是一個Promise
,因此咱們在最外邊也添加await
的字樣
也就是說咱們每次reduce
都會返回一個新的Promise
對象,在對象內部都會獲取上次Promise
的結果。
咱們調用reduce
實際上獲得的是相似這樣的一個Promise
對象:
new Promise(resolve => {
let item = 3
new Promise(resolve => {
let item = 2
new Promise(resolve => {
let item = 1
Promise.resolve(0).then(result => resolve(item + result))
}).then(result => resolve(item + result))
}).then(result => resolve(item + result))
})
複製代碼
這個就沒什麼好說的了。。跟reduce
只是執行順序相反而已
forEach
,這個應該是用得最多的遍歷方法了,對應的函數簽名:
callback
,對每個元素進行調用的函數
currentValue
,當前元素index
,當前元素下標array
,調用forEach
的數組引用thisArg
,一個可選的回調函數this
指向咱們有以下的操做:
// 獲取數組元素求平方後的值
[1, 2, 3].forEach(item => {
console.log(item ** 2)
})
// > 1
// > 4
// > 9
複製代碼
普通版本咱們是能夠直接這麼輸出的,可是若是遇到了Promise
// 獲取數組元素求平方後的值
[1, 2, 3].forEach(async item => {
console.log(item ** 2)
})
// > nothing
複製代碼
forEach
並不關心回調函數的返回值,因此forEach
只是執行了三個會返回Promise
的函數
因此若是咱們想要獲得想要的效果,只可以本身進行加強對象屬性了:
Array.prototype.forEachSync = async function (callback, thisArg) {
for (let [index, item] of Object.entries(this)) {
await callback(item, index, this)
}
}
await [1, 2, 3].forEachSync(async item => {
console.log(item ** 2)
})
// > 1
// > 4
// > 9
複製代碼
await
會忽略非Promise
值,await 0
、await undefined
與普通代碼無異
filter
做爲一個篩選數組用的函數,一樣具備遍歷的功能:
函數簽名同forEach
,可是callback
返回值爲true
的元素將被放到filter
函數返回值中去。
咱們要進行一個奇數的篩選,因此咱們這麼寫:
[1, 2, 3].filter(item => item % 2 !== 0)
// > [1, 3]
複製代碼
而後咱們改成Promise
版本:
[1, 2, 3].filter(async item => item % 2 !== 0)
// > [1, 2, 3]
複製代碼
這會致使咱們的篩選功能失效,由於filter
的返回值匹配不是徹底相等的匹配,只要是返回值能轉換爲true
,就會被認定爲經過篩選。
Promise
對象必然是true
的,因此篩選失效。
因此咱們的處理方式與上邊的forEach
相似,一樣須要本身進行對象加強 但咱們這裏直接選擇一個取巧的方式:
Array.prototype.filterSync = async function (callback, thisArg) {
let filterResult = await Promise.all(this.map(callback))
// > [true, false, true]
return this.filter((_, index) => filterResult[index])
}
await [1, 2, 3].filterSync(item => item % 2 !== 0)
複製代碼
咱們能夠直接在內部調用map
方法,由於咱們知道map
會將全部的返回值返回爲一個新的數組。
這也就意味着,咱們map
能夠拿到咱們對全部item
進行篩選的結果,true
或者false
。
接下來對原數組每一項進行返回對應下標的結果便可。
some
做爲一個用來檢測數組是否知足一些條件的函數存在,一樣是能夠用做遍歷的
函數簽名同forEach
,有區別的是當任一callback
返回值匹配爲true
則會直接返回true
,若是全部的callback
匹配均爲false
,則返回false
咱們要判斷數組中是否有元素等於2
:
[1, 2, 3].some(item => item === 2)
// > true
複製代碼
而後咱們將它改成Promise
[1, 2, 3].some(async item => item === 2)
// > true
複製代碼
這個函數依然會返回true
,可是卻不是咱們想要的,由於這個是async
返回的Promise
對象被認定爲true
。
因此,咱們要進行以下處理:
Array.prototype.someSync = async function (callback, thisArg) {
for (let [index, item] of Object.entries(this)) {
if (await callback(item, index, this)) return true
}
return false
}
await [1, 2, 3].someSync(async item => item === 2)
// > true
複製代碼
由於some
在匹配到第一個true
以後就會終止遍歷,因此咱們在這裏邊使用forEach
的話是在性能上的一種浪費。
一樣是利用了await
會忽略普通表達式的優點,在內部使用for-of
來實現咱們的需求
以及咱們最後的一個every
函數簽名一樣與forEach
同樣,
可是callback
的處理仍是有一些區別的:
其實換一種角度考慮,every
就是一個反向的some
some
會在獲取到第一個true
時終止
而every
會在獲取到第一個false
時終止,若是全部元素均爲true
,則返回true
咱們要斷定數組中元素是否所有大於3
[1, 2, 3].every(item => item > 3)
// > false
複製代碼
很顯然,一個都沒有匹配到的,並且回調函數在執行到第一次時就已經終止了,不會繼續執行下去。
咱們改成Promise
版本:
[1, 2, 3].every(async => item > 3)
// > true
複製代碼
這個必然是true
,由於咱們判斷的是Promise
對象
因此咱們拿上邊的someSync
實現稍微修改一下:
Array.prototype.everySync = async function (callback, thisArg) {
for (let [index, item] of Object.entries(this)) {
if (!await callback(item, index, this)) return false
}
return true
}
await [1, 2, 3].everySync(async item => item === 2)
// > false
複製代碼
當匹配到任意一個false
時,直接返回false
,終止遍歷。
關於數組的這幾個遍歷方法。
由於map
和reduce
的特性,因此是在使用async
時改動最小的函數。
reduce
的結果很像一個洋蔥模型
但對於其餘的遍歷函數來講,目前來看就須要本身來實現了。
四個*Sync
函數的實現:github.com/Jiasm/noteb…