# js踩坑记录
> 专门用来踩坑用
1. 在foreach中await功能使用异常问题
    1. 问题描述,在数组的 `forEach` 回调函数中使用 `await` 去进行数据处理时会出现任务同步被执行问题
    2. 解决方法
       1. 改造 `forEach` 函数
            > 之间修改 forEach 函数,改造成async await形式的回调函数

            ```
            async function asyncForEach(array, callback) {
                for (let index = 0; index < array.length; index++) {
                    await callback(array[index], index, array)
                }
            }
            ```
       2. 使用原生的 `for` 循环去实现
            > 改用 for in 或者是 for 等去实现
    3. 问题原因
       1. [mdn](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) 网站解释
            
            `forEach` 函数等同于一个回调函数,如下面的代码示意

            ```
            Array.prototype.forEach = function (callback) {
                // this represents our array
                for (let index = 0; index < this.length; index++) {
                    // We call the callback for each entry
                    callback(this[index], index, this)
                }
            }

            ```
    4. 参考解决的文章
     
       抄袭来源 http://objcer.com/2017/10/12/async-await-with-forEach/