-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time))
}
async function delayedLog(item) {
await delay(1000)
console.log('item')
}
串行
可以保证异步逻辑的顺序
async processsArray(array) {
for (const item of array) {
await delayedLog(item)
}
console.log('Done !')
}
processArray([1, 2, 3])
// 1
// 2
// 3
// Done !
并行
async function processArray(array) {
const promises = array.map(delayedLog)
await Promise.all(promises)
console.log('Done !')
}
注意:Array.prototype.forEach() 并不能实现。