JS Promise的简单实现

//constructor
var Promise = function() {
    this.callbacks = [];
}


Promise.prototype = {
    construct: Promise,
    resolve: function(result) {
        this.complete("resolve", result);
    },


    reject: function(result) {
        this.complete("reject", result);
    },


    complete: function(type, result) {
        while (this.callbacks[0]) {
            this.callbacks.shift()[type](result);
        }
    },


    then: function(successHandler, failedHandler) {
        this.callbacks.push({
            resolve: successHandler,
            reject: failedHandler
        });


        return this;
    }
}


// test
var promise = new Promise();

阅读更多
文章标签: js
个人分类: js
上一篇apache WordPress 伪静态.htaccess出现403 Forbidden Error
下一篇apache 301重定向
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭