关闭

JS Promise的简单实现

标签: js
344人阅读 评论(0) 收藏 举报
分类:
//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();

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9634次
    • 积分:236
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类