首页 > 其他 > 详细

PromiseThen 横向代码编辑

时间:2019-04-23 16:20:10      阅读:118      评论:0      收藏:0      [点我收藏+]

var PromiseThen = function(){

var fns = [];

this.then = function(fn){
  fns.push(fn);
  return this;
}

this.main = function (p){
  var
  //工序id
  i = -1,
  //是否停止
  isStop = false;
  //继续流程
  resolve = function(o){
    if( isStop ) return;
    p = o;
    i++;
    fns[i] && fns[i].call({suc: resolve},p,resolve);
  };
  //触发第一道工序
  resolve(p);
  return this;
}

this.start = function(param){
  //新建工序
  return new this.main();
}

//清除掉
this.remove = function(index){
  if( !index ){
    fns = [];
  }
}

//插入工序
this.add = function(fn,index){
  fns.splice(index,0,fn);
}

}

var b = new PromiseThen()
//第一道工序
.then(function(p,suc){
setTimeout(function(){ suc(); },5000)
})
//第二道工序
.then(function(p,suc){
debugger; suc();
})
//第三道工序
.then(function(p,suc){
debugger; suc();
})
//开始
.start();

PromiseThen 横向代码编辑

原文:https://www.cnblogs.com/liao1992/p/10756995.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!