本身照着Promise/A范例写的Promise库

试图写了一下,也许测试了一下,没有用专业的测试东西测试….

代码觉得照样写的比较好懂的,本人程度较差,假如测试有题目,迎接复兴指出
地点: https://github.com/julyL/Code…

(function(global) {
    function isFunction(val) {
        return typeof val == 'function';
    }

    function isObject(val) {
        return typeof val == 'object';
    }

    function asyncExcute(fn) {
        return function() {
            setTimeout(fn); // 统一实行行列中,原生Promise是先于setTimeout完成的,本身完成临时用setTimeout模仿
        }
    }
    function Promise(fn) {
        if (!isFunction(fn) || this instanceof Promise == false) {
            throw new TypeError("Promise必需传入function而且new组织")
        }
        this.status = 'pending';
        this.value = undefined; //  promise状况转变以后通报的值
        this.thenPromise = undefined; //实行then以后返回的promise
        this.resolveQueue = []; //
        this.rejectQueue = []; //   
        var re = asyncExcute(function(resolveData) { this._resolve(resolveData) }.bind(this)), //这里必需异步处置惩罚,不然then函数实行之前能够就已实行了_resolve,但这时候then中函数还未到场resolveQueue中
            rj = asyncExcute(function(resolveData) { this._reject(resolveData) }.bind(this));
        try {
            fn(re, rj)
        } catch (error) {
            this.status = 'reject';
            this.value = error;
            asyncExcute(function(){this._reject(error)});         //  new Promise(()=>{ 出现非常... }).then(refn,rjfn);    出现非常时then还未实行, rjfn还未到场到rejectQueue中  
        }
    }
    Promise.prototype.then = function(refn, rjfn) {
        var returnPro = new Promise(function() {});
        this.thenPromise = returnPro;
        this.resolveQueue.push(refn);
        this.rejectQueue.push(rjfn);

        if (this.status == 'resolve') { //实行then时,假如状况已不是pending,则实行响应函数
            this._resolve(this.value);
        }
        if (this.status == 'reject') {
            this._reject(this.value);
        }
        return returnPro;
    }
    Promise.prototype._resolve = function(resolveData) {
        var handle,
            returnVal;
        this.status = "resolve";
        this.value = resolveData;
        while (this.resolveQueue.length > 0) { //2.2.6  当 promise 胜利实行时,一切 onFulfilled 需根据其注册递次依次回调
            handle = this.resolveQueue.shift();
            if (!isFunction(handle)) { //不是函数  2.1.1 onFulfilled 不是函数,其必需被疏忽
                this.thenPromise.value = resolveData;
                this.thenPromise.status = 'resolve';
                this.thenPromise._resolve();
                return;
            }
            try { //假如 onFulfilled 或许 onRejected 抛出一个非常 e ,则 promise2 必需谢绝实行,并返回拒因 e
                returnVal = handle(resolveData);
            } catch (error) {
                this.thenPromise.status = 'reject';
                this.thenPromise.value = error;
                this.thenPromise._reject();
            }
            if (isObject(returnVal || isFunction(returnVal))) { //假如返回值为对象或许函数
                if (returnVal == this.thenPromise) { //假如 promise 和 x 指向统一对象,以 TypeError 为据因谢绝实行 promise
                    this.thenPromise.status = 'reject';
                    this.thenPromise.value = new TypeError('[[Resolve]](promise, x),promise 和 x 不能指向统一对象');
                } else if (returnVal instanceof Promise) { //假如 x 为 Promise ,则使 promise 接收 x 的状况
                    try {
                        then.call(returnVal, this.thenPromise._resolve.bind(this.thenPromise), this.thenPromise._reject.bind(this.thenPromise));
                    } catch (error) {
                        this.thenPromise.status = 'reject';
                        this.thenPromise.value = error;
                        this.thenPromise._reject();
                    }
                } else { //假如 x 为对象或许函数
                    try { //假如取 x.then 的值时抛出毛病 e ,则以 e 为据因谢绝 promise
                        var then = returnVal.then;
                        if (isFunction(then)) {
                            then.call(returnVal, this.thenPromise._resolve.bind(this.thenPromise), this.thenPromise._reject.bind(this.thenPromise));
                        }
                    } catch (error) {
                        this.thenPromise.status = 'reject';
                        this.thenPromise.value = error;
                        this.thenPromise._reject();
                    }
                }
            } else {
                this.thenPromise.value = returnVal;
                this.thenPromise.status = 'resolve';
                this.thenPromise._resolve();
            }
        }
    }

    Promise.prototype._reject = function(resolveData) {
        var handle,
            returnVal;
        this.status = "resolve";
        this.value = resolveData;
        while (this.rejectQueue.length > 0) {     //2.2.6  当 promise 胜利实行时,一切 onFulfilled 需根据其注册递次依次回调
            handle = this.rejectQueue.shift();
            if (!isFunction(handle)) {          //不是函数  2.1.1 onFulfilled 不是函数,其必需被疏忽
                this.thenPromise.value = resolveData;
                this.thenPromise.status = 'resolve';
                this.thenPromise._resolve();
                return;
            }
            try { //假如 onFulfilled 或许 onRejected 抛出一个非常 e ,则 promise2 必需谢绝实行,并返回拒因 e
                returnVal = handle(resolveData);
            } catch (error) {
                this.thenPromise.status = 'reject';
                this.thenPromise.value = error;
                this.thenPromise._reject();
            }
            if (isObject(returnVal || isFunction(returnVal))) { //假如返回值为对象或许函数
                if (returnVal == this.thenPromise) { //假如 promise 和 x 指向统一对象,以 TypeError 为据因谢绝实行 promise
                    this.thenPromise.status = 'reject';
                    this.thenPromise.value = new TypeError('[[Resolve]](promise, x),promise 和 x 不能指向统一对象');
                } else if (returnVal instanceof Promise) { //假如 x 为 Promise ,则使 promise 接收 x 的状况
                    try {
                        then.call(returnVal, this.thenPromise._resolve.bind(this.thenPromise), this.thenPromise._reject.bind(this.thenPromise));
                    } catch (error) {
                        this.thenPromise.status = 'reject';
                        this.thenPromise.value = error;
                        this.thenPromise._reject();
                    }
                } else {      //假如 x 为对象或许函数
                    try {     //假如取 x.then 的值时抛出毛病 e ,则以 e 为据因谢绝 promise
                        var then = returnVal.then;
                        if (isFunction(then)) {
                            then.call(returnVal, this.thenPromise._resolve.bind(this.thenPromise), this.thenPromise._reject.bind(this.thenPromise));
                        }
                    } catch (error) {
                        this.thenPromise.status = 'reject';
                        this.thenPromise.value = error;
                        this.thenPromise._reject();
                    }
                }
            } else {
                this.thenPromise.value = returnVal;
                this.thenPromise.status = 'resolve';
                this.thenPromise._resolve();
            }
        }
    }

    Promise.resolve = function(value) {
        return new Promise(function(re, rj) {
            re(value)
        })
    }
    Promise.reject = function(value) {
        return new Promise(function(re, rj) {
            re(value)
        })
    }
    Promise.all = function(queue) {
        if (Object.prototype.toString.call(queue) != "[object Array]") {
            return;
        }
        var returnPromise = new Promise(function() {}),
            resolveNum = 0;
        for (var i = 0; i < queue.length; i++) {
            queue[i].then(function() {
                resolveNum++;
                if (resolveNum == queue.length) {
                    returnPromise._resolve();
                }
            });
        }
        return returnPromise;
    }
    Promise.race = function(queue) {
        if (Object.prototype.toString.call(queue) != "[object Array]") {
            return;
        }
        var returnPromise = new Promise(function() {}),
            resolveNum = 0;
        for (var i = 0; i < queue.length; i++) {
            queue[i].then(function() {
                returnPromise._resolve();
            });
        }
        return returnPromise;
    }
    global.Promise = Promise;
})(window);

    原文作者:july_L
    原文地址: https://segmentfault.com/a/1190000009134867
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞