手写一个promise

Promise A+ 规范:https://promisesaplus.com/

代码实现

    class myPromise {
        constructor(executor) {
            this.state = 'pending';
            this.value = undefined;
            this.reason = undefined;
            this.onResolvedArray = [];
            this.onRejectedArray = [];
            const resolve = (val) => {
                if (this.state === "pending") {
                    this.state = 'resolved';
                    this.value = val;
                    this._doResolve();
                }
            };
            const reject = (rejectReason) => {
                if (this.state === "pending") {
                    this.state = 'rejected';
                    this.reason = rejectReason;
                    this._doReject();
                }
            };
            try {
                executor(resolve, reject);
            } catch (e) {
                reject(e);
            }
        }

        then(onResolved, onRejected) {
            let promise2;
            promise2 = new myPromise((resolve, reject) => {

                // 遵循 2.2
                const afterResolved = value => {
                    try {
                        onResolved
                            ? resolvePromise(promise2, onResolved(value), resolve, reject)
                            : resolvePromise(promise2, value, resolve, reject);
                    } catch (e) {
                        reject(e);
                    }
                };
                const afterRejected = reason => {
                    try {
                        onRejected
                            ? resolvePromise(promise2, onRejected(reason), resolve, reject)
                            : reject(reason);
                    } catch (e) {
                        reject(e);
                    }
                };

                // 2.1
                switch (this.state) {
                    case "pending":
                        this.onResolvedArray.push(afterResolved);
                        this.onRejectedArray.push(afterRejected);
                        break;
                    case "resolved":
                        this.onResolvedArray.push(afterResolved);
                        this._doResolve();
                        break;
                    case "rejected":
                        this.onRejectedArray.push(afterRejected);
                        this._doReject();
                        break;
                }
            });
            return promise2;
        }

        // 执行所有的 onResolved
        _doResolve() {
            // XXX: 模拟一下microTask
            Promise.resolve().then(() => {
                this.onResolvedArray.forEach(f => f(this.value));
                this.onResolvedArray = [];
            });
        }

        // 执行所有的 onRejected
        _doReject() {
            // XXX: 模拟一下microTask
            Promise.resolve().then(() => {
                this.onRejectedArray.forEach(f => f(this.reason));
                this.onRejectedArray = [];
            });
        }

        // then(null, onRejected) 的别名
        catch(onRejected) {
            return this.then(null, onRejected);
        }

        static resolve(val) {
            return new myPromise((resolve) => {
                resolve(val);
            });
        }

        static reject(reason) {
            return new myPromise((resolve, reject) => {
                reject(reason);
            });
        }

        static all(promiseList) {
            return new myPromise((resolve, reject) => {
                const result = [];
                let count = 0;
                promiseList.forEach((item, index) => {
                    item
                        .then(value => {
                            count++;
                            result[index] = value;
                            if (count === promiseList.length - 1) {
                                resolve(result);
                            }
                        })
                        .catch(err => {
                            reject(err);
                        });
                });
            });
        }

        static race(promiseList) {
            return new myPromise((resolve, reject) => {
                promiseList.forEach(item => {
                    item.then(resolve, reject);
                });
            });
        }
    }

    // 处理onResolve返回promise时的情况
    function resolvePromise(promise, x, resolve, reject) {
        // debugger
        if (promise === x) {
            console.error(new TypeError('promise 循环错误'));
            return;
        }
        try {
            if ((typeof x === "object" || typeof x === "function") && typeof x.then === "function") {
                x.then(
                    value => {
                        resolvePromise(promise, value, resolve, reject);
                    },
                    reason => {
                        reject(reason);
                    }
                );
            } else {
                resolve(x);
            }
        } catch (e) {
            reject(e);
        }
    }

测试

    function test(promiseConstructor) {
        function log(msg) {
            console.log(promiseConstructor.name + '\t\t', msg);
        }

        const testCase = [
            {
                ["测试同步promise"]() {
                    let resolve_immediate = promiseConstructor.resolve('immediate');
                    resolve_immediate.then(value => {
                        log('resolved', value);
                    });
                    let reject_immediate = promiseConstructor.reject('immediate');
                    reject_immediate.catch(value => {
                        log('rejected', value);
                    });
                }
            },
            {
                ["测试异步promise"]() {
                    new promiseConstructor((resolve) => {
                        setTimeout(() => {
                            resolve(1);
                        }, 100);
                    })
                        .then(val => log('resolve', val));
                    new promiseConstructor((resolve, reject) => {
                        setTimeout(() => {
                            reject(2);
                        }, 100);
                    })
                        .catch(val => log('reject1', val));
                }
            },
            {
                ["测试链式调用和throw方法"]() {
                    new promiseConstructor((resolve) => {
                        setTimeout(() => {
                            resolve(1);
                        }, 100);
                    })
                        .then(value => {
                            log(value);
                            return value + 1;
                        })
                        .catch(value => {
                            log('我不应该出现', value);
                            return value + 1;
                        })
                        .then(value => {
                            log(value);
                            throw value + 1;
                        })
                        .catch(value => {
                            log(value);
                            throw value + 1;
                            // return value + 1;
                        })
                        .then(value => {
                            log(value);
                        })
                        .catch(log);
                }
            },
            {
                ["测试返回promise"]() {
                    new promiseConstructor((resolve) => {
                        setTimeout(() => {
                            resolve(1);
                        }, 100);
                    })
                        .then(value => {
                            return new promiseConstructor((resolve) => {
                                setTimeout(() => {
                                    resolve(value + 1);
                                    resolve(value + 1); // 这里重复调用了resolve,但是并不会有什么后果
                                });
                            });
                        })
                        .then(value => {
                            log(value);
                            return value + 1;
                        })
                        .then(value => {
                            return new promiseConstructor((resolve, reject) => {
                                setTimeout(() => {
                                    reject(value + 1);
                                    resolve(value + 1); // 这里重复调用了resolve,但是并不会有什么后果
                                });
                            });
                        })
                        .catch(value => {
                            log(value);
                            return value + 1;
                        });
                }
            },
            {
                ["测试promise.all"]() {
                    promiseConstructor.all([
                        promiseConstructor.resolve(1),
                        promiseConstructor.resolve(2)
                    ])
                        .then(log)
                        .catch((err) => {
                            log(err, '我不出现');
                        });
                    promiseConstructor.all([
                        promiseConstructor.reject(1),
                        promiseConstructor.resolve('我不出现1'),
                        promiseConstructor.reject('我不出现2'),
                    ])
                        .then((err) => {
                            log(err, '我不出现');
                        })
                        .catch(log);
                }
            },
            {
                ["测试promise.race"]() {
                    promiseConstructor.race([
                        new promiseConstructor(resolve => {
                            setTimeout(() => {
                                resolve('我不出现');
                            }, 200);
                        }),
                        new promiseConstructor(resolve => {
                            setTimeout(() => {
                                resolve(1);
                            }, 100);
                        })
                    ])
                        .then(log)
                        .catch((err) => {
                            log(err, '我不出现');
                        });
                    promiseConstructor.race([
                        promiseConstructor.reject(1),
                        promiseConstructor.resolve('我不出现1'),
                        promiseConstructor.reject('我不出现2'),
                    ])
                        .then((err) => {
                            log(err, '我不出现');
                        })
                        .catch(log);
                }
            },
            {
                ["测试循环promise"]() {
                    let a = new promiseConstructor(resolve => {
                        setTimeout(() => {
                            resolve(1);
                        }, 100);
                    })
                        .then(value => {
                            return 2;
                        })
                        .then(value => {
                            log(value, '应该报循环引用错误2');
                            return a;
                        });
                    // 不知道为什么,这里如果加一个.catch或者.then就不会报错了
                    // 原生的promise也是这样
                }
            }
        ];
        for (let i = 0, len = testCase.length; i < len; i++) {
            const item = testCase[i];
            setTimeout(() => {
                const name = Object.keys(item)[0];
                console.group(name);
                item[name]();
                setTimeout(() => {
                    console.groupEnd();
                }, 900);
            }, i * 1000);
        }
    }
    test(myPromise);
    test(Promise);

原文地址:https://www.cnblogs.com/wozho/p/10565931.html

时间: 2024-08-11 09:51:09

手写一个promise的相关文章

放弃antd table,基于React手写一个虚拟滚动的表格

缘起 标题有点夸张,并不是完全放弃antd-table,毕竟在react的生态圈里,对国人来说,比较好用的PC端组件库,也就antd了.即便经历了2018年圣诞彩蛋事件,antd的使用者也不仅不减,反而有所上升. 客观地说,antd是开源的,UI设计得比较美观(甩出其他组件库一条街),而且是蚂蚁金服的体验技术部(一堆p7,p8,p9,基本都是大牛级的)在持续地开发维护,质量可以信任. 不过,antd虽好,但一些组件在某一些场景下,是很不适用的.例如,以表格形式无限滚动地展示大量数据(1w+)时,

利用SpringBoot+Logback手写一个简单的链路追踪

目录 一.实现原理 二.代码实战 三.测试 最近线上排查问题时候,发现请求太多导致日志错综复杂,没办法把用户在一次或多次请求的日志关联在一起,所以就利用SpringBoot+Logback手写了一个简单的链路追踪,下面详细介绍下. 一.实现原理 Spring Boot默认使用LogBack日志系统,并且已经引入了相关的jar包,所以我们无需任何配置便可以使用LogBack打印日志. MDC(Mapped Diagnostic Context,映射调试上下文)是log4j和logback提供的一种

手写一个模块化的 TCP 服务端客户端

前面的博客 基于 socket 手写一个 TCP 服务端及客户端 写过一个简单的 TCP 服务端客户端,没有对代码结构进行任何设计,仅仅是实现了相关功能,用于加深对 socket 编程的认识. 这次我们对整个代码结构进行一下优化,使其模块化,易扩展,成为一个简单意义上的“框架”. 对于 Socket 编程这类所需知识偏底层的情况(OS 协议栈的运作机制,TCP 协议的理解,多线程的理解,BIO/NIO 的理解,阻塞函数的运作原理甚至是更底层处理器的中断.网卡等外设与内核的交互.核心态与内核态的切

手写基于Promise A+规范的Promise

const PENDING = 'pending';//初始态const FULFILLED = 'fulfilled';//初始态const REJECTED = 'rejected';//初始态function Promise(executor){ let self = this;//先缓存当前promise实例 self.status = PENDING;//设置状态 //定义存放成功的回调的数组 self.onResolvedCallbacks = []; //定义存放失败回调的数组 s

Spring系列之手写一个SpringMVC

目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 Spring系列之AOP的原理及手动实现 Spring系列之手写注解与配置文件的解析 引言 在前面的几个章节中我们已经简单的完成了一个简易版的spring,已经包括容器,依赖注入,AOP和配置文件解析等功能.这一节我们来实现一个自己的springMvc. 关于MVC/SpringMVC springMvc是一个基于mvc模式的web框架,SpringMVC框架是一种提供了MVC(模型 - 视图 - 控制器)架

js手写一个实现一个简单的promise__小前端chenMr

1.首先来看一下promise常用场景 function chenPromise (arg) { return new Promise (resolve, reject) { if () { doSomeThing(arg) resolve(val) } else { reject(val) } } }chenPromise(arg).then(((val) => {}, (err) =>{}) 2.了解promise状态 1.常规分三种:PENDING, RESOLVED, REJECTED

css手写一个表头固定

Bootstrap,layui等前端框架里面都对表头固定,表格滚动有实现,偏偏刚入职的公司选择了手动渲染表格,后期又觉得表格数据拉太长想要做表头固定.为了避免对代码改动太大,所以决定手写表头固定 主要遇到的个问题就是固定以后数据表格与表头的对齐问题,也看了很多我文章试下来都不怎么成功,只好自己一点点试 表头固定的一般思路是布两个table,一个放表头,一个放表格体,然后将表格体加上高度height以及overflow-y <div class="content"> <

手写一个IOC容器

链接:https://pan.baidu.com/s/1MhKJYamBY1ejjjhz3BKoWQ 提取码:e8on 明白什么是IOC容器: IOC(Inversion of Control,控制反转).这是spring的核心,贯穿始终.所谓IOC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系. 传统的java代码中,我们需要使用哪个对象,就new一个对象,很正常对吧? 然而,这时出现了一个新思想:IOC(控制反转) 由它创建和管理所有的对象,我们需要的时

手写一个词法分析器

前言 最近大部分时间都在撸 Python,其中也会涉及到将数据库表转换为 Python 中 ORM 框架的 Model,但我们并没有找到一个合适的工具来做这个意义不大的"体力活",所以每次新建表后大家都是根据自己的表结构手写一遍 Model. 一两张表还好,一旦 10 几张表都要写一遍时那痛苦只有自己知道:这时程序员的 slogan 再次印证:一切毫无意义的体力劳动终将被计算机取代. intellij plugin 既然没有现成的工具那就自己写一个吧,演示效果如下: 考虑到我们主要是用