手写一个promise
Promise A+ 规范:https://promisesaplus.com/
注:以下代码没有通过 promises-aplus-tests 的全部测试,但基本功能还是全的( 测试结果: 864 passing, 8 failing)
另外可以参考这个指南中的 Promise实现
代码实现
// Promise resolve_100+ 规范: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 {
                    typeof onResolved === "function"
                        ? resolvePromise(promise2, onResolved(value), resolve, reject)
                        : resolvePromise(promise2, value, resolve, reject);
                } catch (e) {
                    reject(e);
                }
            };
            const afterRejected = reason => {
                try {
                    typeof onRejected === "function"
                        ? 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(() => {
            // if(this.onRejectedArray.length ===0) console.error('Uncaught myPromise', this.reason && this.reason.message || this.reason);
            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);
            });
        });
    }
    // 下面两个是为了测试才加的
    static deferred() {
        let dfd = {};
        dfd.promise = new myPromise(function (resolve, reject) {
            dfd.resolve = resolve;
            dfd.reject = reject;
        });
        return dfd;
    }
    static defer() {
        let dfd = {};
        dfd.promise = new myPromise(function (resolve, reject) {
            dfd.resolve = resolve;
            dfd.reject = reject;
        });
        return dfd;
    }
}
// 处理onResolve返回promise时的情况
function resolvePromise(promise, x, resolve, reject) {
    let called = false;
    if (promise === x) {
        if (called) return;
        called = true;
        reject(new TypeError('promise 循环错误'));
        console.error('promise 循环错误');
        return;
    }
    try {
        if (Object.prototype.toString.call(x) === "[object Object]" || typeof x === "function") {
            const then = x.then;
            if (typeof then === "function") {
                then.call(
                    x,
                    value => {
                        if (called) return;
                        called = true;
                        resolvePromise(promise, value, resolve, reject);
                    },
                    reason => {
                        if (called) return;
                        called = true;
                        reject(reason);
                    }
                );
            } else {
                if (called) return;
                called = true;
                resolve(x);
            }
        } else {
            if (called) return;
            called = true;
            resolve(x);
        }
    } catch (e) {
        if (called) return;
        called = true;
        reject(e);
    }
}
try {
    module.exports = myPromise;
} catch (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);
手写一个promise的更多相关文章
- 【原】手写一个promise
		上一篇文章中,我们介绍了Promise的基本使用,在这篇文章中,我们试着自己来写一个Promise,主要是学习Promise的内部机制,学习它的编程思想. !!!备注:本文写的不好,仅供自己学习之用, ... 
- 掘金转载-手写一个Promise
		目录 一 什么是Promise ? 二 Promises/A+ 规范 2.1 术语 2.2 基本要求 2.2.1. Promise的状态 2.2.2. Then 方法 2.3 简易版实践 2.4 进一 ... 
- 手写一个Promise/A+,完美通过官方872个测试用例
		前段时间我用两篇文章深入讲解了异步的概念和Event Loop的底层原理,然后还讲了一种自己实现异步的发布订阅模式: setTimeout和setImmediate到底谁先执行,本文让你彻底理解Eve ... 
- 面试----你可以手写一个promise吗
		参考:https://www.jianshu.com/p/473cd754311f <!DOCTYPE html> <html> <head> <meta c ... 
- 只会用就out了,手写一个符合规范的Promise
		Promise是什么 所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise 是一个对象,从它可以获取异步操作的消息.Prom ... 
- Javascript之我也来手写一下Promise
		Promise太重要了,可以说是改变了JavaScript开发体验重要内容之一.而Promise也可以说是现代Javascript中极为重要的核心概念,所以理解Promise/A+规范,理解Promi ... 
- 『练手』手写一个独立Json算法 JsonHelper
		背景: > 一直使用 Newtonsoft.Json.dll 也算挺稳定的. > 但这个框架也挺闹心的: > 1.影响编译失败:https://www.cnblogs.com/zih ... 
- 教你如何使用Java手写一个基于链表的队列
		在上一篇博客[教你如何使用Java手写一个基于数组的队列]中已经介绍了队列,以及Java语言中对队列的实现,对队列不是很了解的可以我上一篇文章.那么,现在就直接进入主题吧. 这篇博客主要讲解的是如何使 ... 
- 【spring】--  手写一个最简单的IOC框架
		1.什么是springIOC IOC就是把每一个bean(实体类)与bean(实体了)之间的关系交给第三方容器进行管理. 如果我们手写一个最最简单的IOC,最终效果是怎样呢? xml配置: <b ... 
随机推荐
- python面向对象三大特性之一封装
			一.什么是封装 在程序设计中,封装(Encapsulation)是对具体对象的一种抽象,即将某些部分隐藏起来,在程序外部看不到,其 含义是其他程序无法调用. 要了解封装,离不开“私有化”,就是将类或者 ... 
- 【JavaScript框架封装】公共框架的封装
			/* * @Author: 我爱科技论坛 * @Time: 20180706 * @Desc: 实现一个类似于JQuery功能的框架 // 公共框架 // 种子模块:命名空间.对象扩展.数组化.类型的 ... 
- MVC架构之delegate
			Qt的MVC架构可以实现很多数据显示的功能,本次主要对代理进行一个总结: 重实现QStyledItemDelegate类,实现自定义类. (1)ComboxDelegate.h #ifndef COM ... 
- fun(int **p)的使用
			#include <iostream>using namespace std;void fun(int **p){ cout << p[0][0] << endl; ... 
- C和C++ const的声明差异
			当在C源代码文件中将变量声明为const时,可以通过以下方式实现: const int i = 2; 然后,可以在另一个模块中使用此变量,如下表示: extern const int i; 但若要获取 ... 
- BA-WG-泰豪发电机
			泰豪发电机的控制主板有2个端口,一个是RS232端口,一个是RS485端口,通常接网关需要将这个RS485的端口调整为modbus协议输出,再将modbus协议通过网关转换为bacnet / ip协议 ... 
- 使用postman 测试restful接口
			前提: 在chrome网上应用商店安装postman 和 Postman Interceptor. 如下图: 使用postman的时候,最后开启postman Interceptor,如下图: 然后启 ... 
- 由free命令想到的
			root@xdj-Z9PA-D8-Series:~# free -m total used free shared buffers cached Mem: 15977 1683 14293 0 132 ... 
- grpc mvn protobuf:compile 过程
			grpc mvn protobuf:compile 过程 编写代码之后,直接使用 mvn protobuf:compile会报错,木有protoc.exe文件: 可以使用Terminal输入mvn命令 ... 
- uva 10061(数学)
			题解:题目要在b进制下输出的是一个数字阶乘后有多少个零,然后输出一共同拥有多少位.首先计算位数,log(n)/log(b) + 1就是n在b进制下有多少位,而log有个公式就是log(M×N) = l ... 
