-
-
Save jacintoface/2e0e023eefe689ab46f6546bddd488c5 to your computer and use it in GitHub Desktop.
for循环和forEach在async的区别
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
for循环对await的请求是按先后顺序的,forEach的请求是并发请求的 | |
function request () { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve('666') | |
}, 1000) | |
}) | |
} | |
async function geuli () { | |
let arr = [request,request,request,request,request,request] | |
// arr.forEach(async (value) => { | |
// await value() | |
// }) | |
// for(let i = 0; i < arr.length; i++) { | |
// let ret = await arr[i]() | |
// console.log(ret) //1s后每隔1s打印一次666 | |
// } | |
arr.forEach(async (value) => { | |
let ret = await value() | |
console.log(ret) //1s后同时打印6个 666 | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment