![https://zhuanlan.zhihu.com/p/53599723
](https://upload-images.jianshu.io/upload_images/4985324-026d1261b27f866b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![foo 函数不是一个纯函数,因为它返回的结果依赖于外部变量 a,我们在不知道 a 的值的情况下,并不能保证 foo(2) 的返回值是 3。
](https://upload-images.jianshu.io/upload_images/4985324-ac32f7d0691c4330.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
function test() {
let arr = [3, 2, 1]
arr.forEach(async item => {
const res = await fetch(item)
console.log(res)
})
console.log('end')
}
function fetch(x) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(x)
}, 500 * x)
})
}
test()
![https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill
](https://upload-images.jianshu.io/upload_images/4985324-84364184dfb803a4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)