实现一个Promise.all

用js自己实现一个Promise.all

let promiseAll = (promises) => {
    return new Promise((resolve, reject) => {
        // 用来存储每个promise的返回值
        let values = new Array(promises.length);
        // 当前已经完成了几个promise
        let finishCount = 0;

        for (let i = 0; i < promises.length; ++i) {
            let promise = promises[i];
            promise.then(val => {
                values[i] = val;
                ++finishCount;
                if (finishCount === promises.length) {
                  resolve(values);
                }
            }).catch(err => {
              reject(err)
            })
        }
    });
};
posted @ 2019-02-11 17:46  赵康  阅读(1450)  评论(0编辑  收藏  举报