Skip to content

Instantly share code, notes, and snippets.

@yangfch3
Created August 18, 2018 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangfch3/cfb631476e81fe35a1f9c8a1ff7edd83 to your computer and use it in GitHub Desktop.
Save yangfch3/cfb631476e81fe35a1f9c8a1ff7edd83 to your computer and use it in GitHub Desktop.
循环与 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