Implement Deferred object without using jquery

前端 未结 2 407
梦毁少年i
梦毁少年i 2021-02-04 16:38

I want to implement basic Deferred object without using jQuery. Here i will be implementing only done and fail callbacks, with resolve and reject functions. and ofCourse associa

2条回答
  •  伪装坚强ぢ
    2021-02-04 16:48

    function Deferred(){
      this._done = [];
      this._fail = [];
    }
    Deferred.prototype = {
      execute: function(list, args){
        var i = list.length;
    
        // convert arguments to an array
        // so they can be sent to the
        // callbacks via the apply method
        args = Array.prototype.slice.call(args);
    
        while(i--) list[i].apply(null, args);
      },
      resolve: function(){
        this.execute(this._done, arguments);
      },
      reject: function(){
        this.execute(this._fail, arguments);
      }, 
      done: function(callback){
        this._done.push(callback);
      },
      fail: function(callback){
        this._fail.push(callback);
      }  
    }
    
    
    var v;
    
    var setVal = function() {
        var d = new Deferred();
        setTimeout(function() {
            v = 'a value';
            d.resolve(this);
        }, 5000);
        return d;
    };
    
    setVal().done(function() {
        console.log('all done :' + v);
    });
    

提交回复
热议问题