• 原生代码实现Promise


    function Promise(fn){
      //需要成功以及成功时的回调
      var state = 'pending';
      var doneList = [];
      this.then = function(done){
        switch(state){
          case "pending":
            doneList.push(done);
            return this;
            break;
          case 'fulfilled':
            done();
            return this;
            break;
        }
      }
      function resolve(newValue){
        state = "fulfilled";
        setTimeout(function(){
          var value = newValue;
          //执行resolve时,我们会尝试将doneList数组中的值都执行一遍
          //当遇到正常的回调函数的时候,就执行回调函数
          //当遇到一个新的promise的时候,就将原doneList数组里的回调函数推入新的promise的doneList,以达到循环的目的
          for (var i = 0;i<doneList.length;i++){
            var temp = doneList[i](value)
            if(temp instanceof Promise){
                var newP =  temp;
                for(i++;i<doneList.length;i++){
                    newP.then(doneList[i]);
                }
            }else{
                value = temp;
            }
          }
        },0);
      }
      fn(resolve);
    }
    var p = function (){
        return new Promise(function(resolve){
            setTimeout(function(){
              resolve('p 的结果');
            }, 100);
        });
    }
    var p2 = function (input){
        return new Promise(function(resolve){
            setTimeout(function(){
                console.log('p2拿到前面传入的值:' + input)
                resolve('p2的结果');
            }, 100);
        });
    }
    p()
    .then(function(res){console.log('p的结果:' + res); return 'p then方法第一次返回'})
    .then(function(res){console.log('p第一次then方法的返回:'+res); return 'p then方法第二次返回'})
    .then(p2)
    .then(function(res){console.log('p2的结果:' + res)});

     

    加入reject版本:

    function Promise(fn){
      var state = 'pending';
      var doneList = [];
      var failList= [];
      this.then = function(done ,fail){
        switch(state){
          case "pending":
            doneList.push(done);
            //每次如果没有推入fail方法,我也会推入一个null来占位
            failList.push(fail || null);
            return this;
            break;
          case 'fulfilled':
            done();
            return this;
            break;
          case 'rejected':
            fail();
            return this;
            break;
        }
      }
      function resolve(newValue){
        state = "fulfilled";
        setTimeout(function(){
          var value = newValue;
          for (var i = 0;i<doneList.length;i++){
            var temp = doneList[i](value);
            if(temp instanceof Promise){
                var newP =  temp;
                for(i++;i<doneList.length;i++){
                    newP.then(doneList[i],failList[i]);
                }
            }else{
                value = temp;
            }
          }
        },0);
      }
      function reject(newValue){
        state = "rejected";
        setTimeout(function(){
          var value = newValue;
          var tempRe = failList[0](value);
          //如果reject里面传入了一个promise,那么执行完此次的fail之后,将剩余的done和fail传入新的promise中
          if(tempRe instanceof Promise){
            var newP = tempRe;
            for(i=1;i<doneList.length;i++){
                newP.then(doneList[i],failList[i]);
            }
          }else{
            //如果不是promise,执行完当前的fail之后,继续执行doneList
            value =  tempRe;
            doneList.shift();
            failList.shift();
            resolve(value);
          }
        },0);
      }
      fn(resolve,reject);
    }
    var p = function (){
        return new Promise(function(resolve,reject){
            setTimeout(function(){
              reject('p 的结果');
            }, 100);
        });
    }
    var p2 = function (input){
        return new Promise(function(resolve){
            setTimeout(function(){
                console.log('p2拿到前面传入的值:' + input)
                resolve('p2的结果');
            }, 100);
        });
    }
    p()
    .then(function(res){console.log('p的结果:' + res); return 'p then方法第一次返回'},function(value){console.log(value);return 'p then方法第一次错误的返回'})
    .then(function(res){console.log('p第一次then方法的返回:'+res); return 'p then方法第二次返回'})
    .then(p2)
    .then(function(res){console.log('p2的结果:' + res)});

     

  • 相关阅读:
    The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make s
    ScrollView 定位
    Fragment获取Activity,Activity获取Fragment
    Popupwindow全屏问题
    bzoj千题计划310:bzoj5285: [Hnoi2018]寻宝游戏(思维题+哈希)
    bzoj千题计划309:bzoj4332: JSOI2012 分零食(分治+FFT)
    2016vijos 1-3 兔子的晚会(生成函数+倍增FWT)
    bzoj千题计划308:bzoj4589: Hard Nim(倍增FWT+生成函数)
    bzoj千题计划307:bzoj5248: [2018多省省队联测]一双木棋
    cdqz2017-test10-加帕里图书馆(区间DP & 简单容斥)
  • 原文地址:https://www.cnblogs.com/thaiwx/p/7515938.html
Copyright © 2020-2023  润新知