美文网首页
手写 Promise - 串联 Promise

手写 Promise - 串联 Promise

作者: bestCindy | 来源:发表于2020-08-17 22:25 被阅读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");
        linkPromise = Symbol("linkPromise");

    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);
            }
        }

        [linkPromise](thenable, catchable) {
            function exec(data, handler, resolve, reject) {
                try {
                    const result = handler(data);
                    if (result instanceof MyPromise) {
                        result.then(d => {
                            resolve(d);
                        }, err => {
                            reject(err);
                        });
                    } else {
                        resolve(result);
                    };
                } catch (err) {
                    reject(err);
                };
            };

            return new MyPromise((resolve, reject) => {
                this[settleHandle](data => {
                    exec(data, thenable, resolve, reject);
                }, RESOLVED, this[thenables]);

                this[settleHandle](reason => {
                    exec(reason, catchable, resolve, reject);
                }, RESOLVED, this[cachables]);
            });
        }
        
        then(thenable, catchable) {
            return this[linkPromise](thenable, catchable);
        }

        catch(cachable) {
            this[settleHandle](cachable, REJECTED, this[cachables]);
            return this[linkPromise](undefined, catchable);
        }
    };
})();

const pro = new MyPromise((resolve, reject) => {
    setTimeout(() => {
        resolve(1);
    }, 3000);
});

pro.then(data => {
    console.log(1, data);
    return 123;
}).then(data => {
    console.log(2, data);
    return new MyPromise(resolve => {
        resolve(456);
    });
}).then(data => {
    console.log(3, data);
    return 789;
});

相关文章

  • 手写 Promise - 串联 Promise

  • 手写Promise

    手写 Promise 我们会通过手写一个符合 Promise/A+ 规范的 Promise 来深入理解它,并且手写...

  • 纯手写实现自己的nodejs promise 库

    纯手写实现自己的nodejs promise 库什么是Promise?promise 链Async/Await后续...

  • 手写Promise

    $ 正常的promise用法   $ 手写的Promise   # 测试可行性

  • Promise 的串联

    当后续的 Promise 需要用到之前的 Promise 的处理结果时,需要 Promise 的串联 Promis...

  • Promise 串联

    《深入理解ES6》阅读随笔 Promise 作为 JavaScript 中异步编程的解决方案,除了其本身具有基本的...

  • 手写 Promise 系列 --- 3

    在前两篇(手写 Promise 系列 --- 1)和(手写 Promise 系列 ---2) 中,达成了3个目标 ...

  • 手写promise

    手写promise 带大家手写一个 promis。在手写之前我会先简单介绍一下为什么要使用promise、prom...

  • 手写基础 promise

    1. 前言 玩下吧 手写 promise,看看能写成啥样 2. promise 基础结构 3. 手写`promi...

  • 2020前端基础大纲(20200202)

    2020前端基础大纲1、promise 原理 promise.all 可以手写出大概。(async awa...

网友评论

      本文标题:手写 Promise - 串联 Promise

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