美文网首页
手写 promise - 后续处理

手写 promise - 后续处理

作者: bestCindy | 来源:发表于2020-08-16 23:52 被阅读0次
    const MyPromise = (() => {
        const PENDING = "pending",
            RESOLVED = "resolved",
            REJECTED = "rejected",
            //不能让外部访问
            PromiseValue = Symbol("PromiseValue"),//状态数据
            PromiseStatus = Symbol("PromiseStatus"),//当前状态
            changeStatus = Symbol("changeStatus"),
            thenables = Symbol("thenables"),
            cachables = Symbol("cachables"),
            settleHandle = Symbol("settleHandle");
    
        return class MyPromise {
            [changeStatus](newStatus, newValue, queue) {
                if (this[PromiseStatus] != PENDING) {
                    //状态无法变更
                    return;
                };
                this[PromiseStatus] = newStatus;
                this[PromiseValue] = newValue;
                //执行响应队列中的函数
                queue.forEach(handler => handler(newValue));
            };
    
            // executor 未决阶段(pending 状态)下的处理函数
            constructor(executor) {
                this[PromiseStatus] = PENDING;
                this[PromiseValue] = undefined;
    
                this[thenables] = [];//后续处理函数的数组 --> resolved
                this[cachables] = [];//后续处理函数的数组 --> rejected
    
                const resolve = data => {
                    this[changeStatus](RESOLVED, data, this[thenables]);
                };
    
                const reject = reason => {
                    this[changeStatus](REJECTED, reason, this[cachables]);
                };
    
                try {
                    executor(resolve, reject);
                } catch (err) {
                    reject(err);
                };
            };
    
            // handler 后续处理函数
            // immediatelyStatus 需要立即执行的状态
            // queue 作业队列 
            [settleHandle](handler, immediatelyStatus, queue) {
                if (typeof handler !== "function") return;
                
                if(this[PromiseStatus] == immediatelyStatus) {
                    setTimeout(() => {
                        handler(this[PromiseValue])
                    }, 0);
                } else {
                    queue.push(handler);
                }
            }
            
            then(thenable, cachable) {
                this[settleHandle](thenable, RESOLVED, this[thenables]);
                this.catch(cachable);
            }
    
            catch(cachable) {
                this[settleHandle](cachable, REJECTED, this[cachables]);
            }
        };
    })();
    
    const pro = new MyPromise((resolve, reject) => {
        setTimeout(() => {
            reject(1);
        }, 3000);
    });
    
    pro.then(data => {
        console.log(1, data);
    });
    
    pro.then(data => {
        console.log(2, data);
    });
    
    pro.catch(err => {
        console.log(311, err);
    });
    
    console.log(pro);
    

    相关文章

      网友评论

          本文标题:手写 promise - 后续处理

          本文链接:https://www.haomeiwen.com/subject/fzzodktx.html