美文网首页
八(1)、Promise(周) ------ 2020-02-2

八(1)、Promise(周) ------ 2020-02-2

作者: 自己写了自己看 | 来源:发表于2020-02-29 19:37 被阅读0次

1、什么是Promise:

(1)首先Promise是ES6规范实现的一个JS内置对象,总结:Promise是和Math、Date等对象一样
的内置对象,不需要引入什么的;
(2)Promise能用来干什么:Promise可以用来管控异步编程代码;

2、通过原生JS实现简单的Promise原理,理解Promise如何管控异步编程:

// 实现基础的then方法:
class Promise {
    constructor(excutorCallBack) {
        this.status = 'pending';
        this.value = undefined;

        // 存放then方法的两个参数
        this.fullfilledAry = [];
        this.rejectedAry = [];
        
        // Promise内置resolve函数
        let resolveFn = result => {
            setTimeout(() => {
                if (this.status !== 'pending') return;
                this.status = 'fulfilled';
                this.value = result;
                this.fullfilledAry.forEach(item => {
                    item(this.value);
                })
            }, 0);
        }
        // Promise内置reject函数
        let rejectFn = reason => {
            setTimeout(() => {
                if (this.status !== 'pending') return;
                this.status = 'rejected';
                this.value = reason;
                this.rejectedAry.forEach(item => {
                    item(this.value);
                })
            }, 0);
        }
        
        // new Promise 实例时传递的函数 立马执行
        excutorCallBack(resolveFn, rejectFn);
    }
    // Promise原型上挂载的 then 方法
    then (fulfilledCallBack, rejectedCallBack) {
        this.fullfilledAry.push(fulfilledCallBack);
        this.rejectedAry.push(rejectedCallBack);
    }
}

module.exports = Promise;

3、在基础版Promise上实现创建Promise实例时,异常报错按照reject状态处理:

// 异常报错
class Promise {
    constructor(excutorCallBack) {
        this.status = 'pending';
        this.value = undefined;

        // 存放then方法的两个参数
        this.fullfilledAry = [];
        this.rejectedAry = [];

        // Promise内置resolve函数
        let resolveFn = result => {
            setTimeout(() => {
                if (this.status !== 'pending') return;
                this.status = 'fulfilled';
                this.value = result;
                this.fullfilledAry.forEach(item => {
                    item(this.value);
                })
            }, 0);
        }
        // Promise内置reject函数
        let rejectFn = reason => {
            setTimeout(() => {
                if (this.status !== 'pending') return;
                this.status = 'rejected';
                this.value = reason;
                this.rejectedAry.forEach(item => {
                    item(this.value);
                })
            }, 0);
        }

        // new Promise 实例时传递的函数 立马执行, 如果 excutorCallBack 中存在异常错误
        // 当做reject处理
        try {
            excutorCallBack(resolveFn, rejectFn);
        } catch (error) {
            rejectFn(error);
        }

    }
    // Promise原型上挂载的 then 方法
    then(fulfilledCallBack, rejectedCallBack) {
        this.fullfilledAry.push(fulfilledCallBack);
        this.rejectedAry.push(rejectedCallBack);
    }
}

module.exports = Promise;

相关文章

网友评论

      本文标题:八(1)、Promise(周) ------ 2020-02-2

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