async/await/iteratorベースの非同期リクエストプール


基于async/await/iterator实现的异步请求池_第1张图片
使用法
//  
import asyncPool from "../lib/index";
/*   
 myIterator
               
 handler
    function       ,  promise,
 workers
    Number    
    
    Array limit promise  
*/
asyncPool(myIterator,handler,workers);

シーンの操作
1、ページに大量の画像が含まれている場合、同時HTTP接続数を制限する
let imgs = [ 'https://www.yuanzhua.cn/img_for_demo/pic_001.jpg','https://www.yuanzhua.cn/img_for_demo/pic_002.jpg', 'https://www.yuanzhua.cn/img_for_demo/pic_003.jpg','https://www.yuanzhua.cn/img_for_demo/pic_004.jpg','https://www.yuanzhua.cn/img_for_demo/pic_005.jpg','https://www.yuanzhua.cn/img_for_demo/pic_006.jpg', 'https://www.yuanzhua.cn/img_for_demo/pic_007.jpg','https://www.yuanzhua.cn/img_for_demo/pic_008.png', 'https://www.yuanzhua.cn/img_for_demo/pic_009.jpg', 'https://www.yuanzhua.cn/img_for_demo/pic_010.jpg', 'https://www.yuanzhua.cn/img_for_demo/pic_011.png', 'https://www.yuanzhua.cn/img_for_demo/pic_012.jpg', 'https://www.yuanzhua.cn/img_for_demo/pic_013.jpg', 'https://www.yuanzhua.cn/img_for_demo/pic_014.jpg','https://www.yuanzhua.cn/img_for_demo/pic_015.jpg'];

function loadImg(url){
            return new Promise((resolve, reject)=>{
                let img=new Image();
                img.src=url;
                img.style.width=
                img.onload=function () {
                    this.width=200;
                    // this.
                    document.body.append(this);
                    resolve()
                }
                img.onerror=function () {
                    reject()
                }
            });
}

Promise.all(asyncPool(imgs.values(),loadImg,5)).then(() => console.log('done'))

Githubオープンソースアドレスhttps://github.com/yuanzhuacn/asyncPool