Js异步并行与串行

一、概述

用普通函数和promise分别实现了两个版本的Js异步并行与串行。最关键的地方在与执行函数的实现,下面贴出代码来记录一把。

二、异步并行

class Executor {

    constructor() {
        this.tasks = [];
        this.tasksPromise = [];
    }
    register(name, fn) {
        this.tasks.push(fn);
    }
    execute(...args) {
        let finalCall =args.pop(),
            index=0,
            done=()=>{
                index++;
                if(index==this.tasks.length){
                    finalCall(); 
                }
            };
        this.tasks.forEach(fn=>fn(...args,done));
    }

    registerPromise(name, fn) {
        this.tasksPromise.push(fn);

    }
    executePromise(...args) {
      this.tasksPromise = this.tasksPromise.map(fn=>fn(...args));
        return Promise.all(this.tasksPromise);
    }
}

let excutor = new Executor();
excutor.register('TaskA',function(data,cb){
    setTimeout(() => {
       console.log(data,'task A....'); 
      cb();
    }, 1000);
})
excutor.register('TaskB',function(data,cb){
    setTimeout(() => {
       console.log(data,'task B....');
       cb();
    }, 1000);
})
excutor.execute('普通异步并行......',function(){
    console.log('普通异步并行 end----');
})
excutor.registerPromise('PromiseA', function (data) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(data,'Promise A....');
            resolve(data);
        }, 3000);
    });

})
excutor.registerPromise('PromiseB', function (data) {

    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(data,'Promise B....');
            resolve(data);
        }, 3000);
    });
})
excutor.executePromise('Promise异步并行......').then(function() {
    console.log('Promise end------');
})
复制代码

三、异步串行

class Executor {

    constructor() {
        this.tasks = [];
        this.tasksPromise = [];
    }
    register(name, fn) {
        this.tasks.push(fn);

    }
    execute(...args) {
        let index = 0;
        let finalCallback = args.pop();

        let next = (data) => {
            if (index == this.tasks.length) return finalCallback(data);
            let task = this.tasks[index];

            if (index == 0) {
                task(...args, next);
            } else {
                task(data, next);
            }
            index++;
        }
        next(...args);
    }

    registerPromise(name, fn) {
        this.tasksPromise.push(fn);

    }
    executePromise(...args) { 
        let index = 0;
        let p;
        let next = (data) => {
            p = this.tasksPromise[index++](data);
            if (index == this.tasksPromise.length) {
                return p;
            } else {
                return p.then(d => next(d));
            }
          }
        return next(...args)
        }
    }

let excutor = new Executor();
excutor.register('TaskA',function(data,cb){
    setTimeout(() => {
       console.log(data); 
       cb('task A....');
    }, 1000);
})
excutor.register('TaskB',function(data,cb){
    setTimeout(() => {
       console.log(data);
       cb('task B....');
    }, 1000);
})
excutor.execute('普通异步串行......',function(data){
    console.log(data);
    console.log('普通异步串行end---');
})
excutor.registerPromise('PromiseA', function (data) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(data);
            resolve('Promise A....');
        }, 3000);
    });

})
excutor.registerPromise('PromiseB', function (data) {
   return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(data);
            resolve('Promise B....');
        }, 3000);
    });
})
excutor.executePromise('Promise异步串行......').then(function (data) {
    console.log(data);
    console.log('Promise异步串行 end------');
})
复制代码

转载于:https://juejin.im/post/5d033dc7e51d454f73356d3e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值