Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
循环与 async-await 搭配使用
/**
* 并行阻塞:
* 一同等待
* 一起爆发式
*/
var arr = [1,2,3]
arr.forEach(async function (value, index) {
await new Promise((resolve, reject) => {
setTimeout(function () {
resolve(true)
}, 1000)
})
console.log(index)
})
console.log('then')
// then
// 1 2 3
for (let i = 0; i < arr.length; i++) {
await new Promise((resolve, reject) => {
setTimeout(function () {
resolve(true)
}, 1000)
})
console.log(i)
}
console.log('then')
// 1
// 2
// 3
// then
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment