javascript 处理并发
//并发请求处理
export const limitLoad = (urls, handler, limit) => {
    let sequence = [].concat(urls);
    let promises = sequence.splice(0, limit).map((url, index) => {
        return handler(url).then(() => {
            return index;
        });
    });
    return sequence.reduce((pCollect, url) => {
        return pCollect
            .then(() => {
                return Promise.race(promises);
            })
            .then(fastestIndex => {
                promises[fastestIndex] = handler(url).then(
                    () => {
                        return fastestIndex;
                    }
                );
            })
            .catch(err => {
                console.error(err);
            });
    }, Promise.resolve())
    .then(() => {
        return Promise.all(promises);
    });
}
确认 取消