Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

低配版promise

$
0
0
function Promise(executor) {
    this.thens = [];
    
    if('function' === typeof executor) {
        executor(this.resolve.bind(this));
    }
};
Promise.prototype = {
    constructor: Promise,
    resolve: function(data) {
        var callback = null;
        for(var i=0,len=this.thens.length; i<len; i++) {
            if('function' === typeof (callback = this.thens[i])) {
                data = callback.call(this, data);
            }
        }
        /*
        while('function' === typeof (callback = this.thens.shift())) {
            data = callback.call(this, data);
        }
        */
    },
    then: function(n) {
        return this.thens.push(n), this;
    }
};

Viewing all articles
Browse latest Browse all 14821

Trending Articles