博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
你与弄懂promise之间可能只差这篇文章(二)
阅读量:5826 次
发布时间:2019-06-18

本文共 4391 字,大约阅读时间需要 14 分钟。

前言:可能存在阐述不准确之处,欢迎指正~

Promise在long time ago就活跃于Javascript社区,受到开发者欢迎,只不过到近几年才被纳入ECMA规范。

我们为什么要使用Promsie?

因为:

我们不希望,过了几个月之后,代码只有上帝才看得懂;

我们不希望,回调代码越写越往右,只能换更大的显示器看;
我们希望,哪怕过了很久,代码依旧逻辑清晰,看懂不费吹灰之力;
我们希望,能够用自己的双手,控制住异步流的动向,就像同步代码一样;
有逼格...
(欢迎补充)

话不多说,上源码(采用es6/es7, 创建一个Commitment类模仿Promise)::

class Commitment {    constructor (executor) {        this.status = "pending";        this.value = void(0);        this.reason = void(0);        this.onResolvedCallbacks = [];        this.onRejectedCallbacks = [];                const resolve = (value) => {            if (value instanceof Promise) {                return value.then(resolve, reject)            }                        setTimeout(() => {                if (this.status === "pending") {                    this.status = "resolved";                    this.value = value;                    this.onResolvedCallbacks.forEach(cb => cb(value));                }            })                    }        const reject = (reason) => {            setTimeout(() => {                if (this.status === "pending") {                    this.status = "rejected";                    this.reason = reason;                    this.onRejectedCallbacks.forEach(cb => cb(reason));                }             })        }                try {            executor(resolve, reject)        } catch (e) {            reject(e)        }    }    then (onFulfilled, onRejected) {        onFulfilled = typeof onFulfilled === "function" ? onFulfilled : value => value;        onRejected = typeof onRejected === "function" ? onRejected : reason => {            throw reason        };        const resolveCommitment = (Commitment2, x, resolve, reject) => {            if (Commitment2 === x) {                return reject(new TypeError("A promise cannot be resolved with itself"))            }            let then, called;            if (x !== null && (typeof x === "object" || typeof x === "function")) {                try {                    then = x.then;                    if (typeof then === "function") {                        then.call(x, y => {                            if (called) return                            called = true;                            resolveCommitment(Commitment2, y, resolve, reject);                        }, r => {                            if (called) return                            called = true;                            reject(r)                        })                    } else {                        resolve(x)                    }                } catch (e) {                    if (called) return                    called = true;                    reject(e)                }            } else {                resolve(x)            }        }        let Commitment2, x;        if (this.status === "resolved") {            Commitment2 = new Commitment((resolve, reject) => {                setTimeout(() => {                    try {                        x = onFulfilled(this.value);                        resolveCommitment(Commitment2, x, resolve, reject)                    } catch (e) {                        reject(e)                    }                 });            })                    }        if (this.status === "rejected") {            Commitment2 = new Commitment((resolve, reject) => {                setTimeout(() => {                    try {                        x = onRejected(this.reason);                        resolveCommitment(Commitment2, x, resolve, reject)                    } catch (e) {                        reject(e)                    }                })            })        }        if (this.status === "pending") {            Commitment2 = new Commitment((resolve, reject) => {                this.onResolvedCallbacks.push((value)=> {                    try {                        x = onFulfilled(value);                        resolveCommitment(Commitment2, x, resolve, reject)                    } catch (e) {                        reject(e)                    }                 })                this.onRejectedCallbacks.push((reason) => {                    try {                        x = onRejected(reason);                        resolveCommitment(Commitment2, x, resolve, reject)                    } catch (e) {                        reject(e)                    }                })            })        }                return Commitment2    }}

转载地址:http://yasdx.baihongyu.com/

你可能感兴趣的文章
windows下Python 3.x图形图像处理库PIL的安装
查看>>
【IL】IL生成exe的方法
查看>>
没有JS的前端:体积更小、速度更快!
查看>>
数据指标/表现度量系统(Performance Measurement System)综述
查看>>
GitHub宣布推出Electron 1.0和Devtron,并将提供无限制的私有代码库
查看>>
论模式在领域驱动设计中的重要性
查看>>
有关GitHub仓库分支的几个问题
查看>>
EAServer 6.1 .NET Client Support
查看>>
锐捷交换机密码恢复(1)
查看>>
Method Swizzling对Method的要求
查看>>
佛祖保佑,永不宕机
查看>>
四、配置开机自动启动Nginx + PHP【LNMP安装 】
查看>>
Linux 目录结构及内容详解
查看>>
OCP读书笔记(24) - 题库(ExamD)
查看>>
.net excel利用NPOI导入oracle
查看>>
$_SERVER['SCRIPT_FLENAME']与__FILE__
查看>>
hive基本操作与应用
查看>>
html5纲要,细谈HTML 5新增的元素
查看>>
Android应用集成支付宝接口的简化
查看>>
[分享]Ubuntu12.04安装基础教程(图文)
查看>>