Created
August 18, 2018 03:17
-
-
Save yangfch3/cfb631476e81fe35a1f9c8a1ff7edd83 to your computer and use it in GitHub Desktop.
循环与 async-await 搭配使用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 并行阻塞: | |
* 一同等待 | |
* 一起爆发式 | |
*/ | |
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