舉例說明:
const arr = [1, 2, 3]
arr.forEach(async item => {
// ...
})
上面的代碼校驗會出一個警告,從 forEach argument 返回的 Promise 被忽略
主要原因:
foreach
裏面的回調函數因爲加了async
的原因,所以默認會返回一個promise
,但是因爲foreach
的實現並沒有返回值,所以導致返回的這個promise
對象沒人去管了
改寫成for...of...
const arr = [1, 2, 3]
for (const item of arr) {
// ...
}