Skip to content

Instantly share code, notes, and snippets.

@jacintoface
Created February 7, 2018 07:02
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 jacintoface/2e0e023eefe689ab46f6546bddd488c5 to your computer and use it in GitHub Desktop.
Save jacintoface/2e0e023eefe689ab46f6546bddd488c5 to your computer and use it in GitHub Desktop.
for循环和forEach在async的区别
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