手写promise(then链的执行原理)(2)

class Promise{

    constructor(excutorCallBack){

        this.status = 'pending'

        this.value = undefined;

        this.fulfilledAry = []; //管控,必须得then执行后才能执行resolveFn方法,成功要做的方法

        this.rejectedAry = []; //失败要做的方法


        // => 执行EXCUTOP

        let resolveFn = (result) =>{

            let timer = setTimeout(()=>{

                if(this.status !== 'pending') return;

                clearTimeout(timer)

                this.status = 'fulfilled';

                this.value = result;

                this.fulfilledAry.forEach(item=>item(this.value))

            },0)

        };

        let rejectFn = (reason) =>{

            let timer = setTimeout(()=>{

                clearTimeout(timer)

                if(this.status !== 'pending') return;

                this.status = 'rejected';

                this.value = reason;

                this.rejectedAry.forEach((item)=>item(this.value))

            },0)

        }

        // => 执行EXCUTOP(异常捕获)

        try{

            excutorCallBack(resolveFn,rejectFn);

        } catch (err){

            // 有异常信息按照rejected状态信息处理

            rejectFn(err);

        }

    }

    then (fulfilledCallBack,rejectedCallBack) {

        // fulfilledCallBack不传递的情况

        typeof fulfilledCallBack !== 'function'? fulfilledCallBack = result =>{

            return result

        } : null;

        typeof rejectedCallBack !== 'function'? rejectedCallBack = reason =>{

            throw new Error(reason.message)

        } : null

        // 返回一个新的Promise实例

        return new Promise((resolve,reject)=>{

            this.fulfilledAry.push(()=>{

                try {

                    let x = fulfilledCallBack(this.value);

                    if(x instanceof Promise){

                        x.then(resolve,reject);

                        return;

                    }

                    resolve(x);

                }catch(err){

                    reject(err)

                }

            });

            this.rejectedAry.push(()=>{

                try{

                    let x = rejectedCallBack(this.value);

                    if(x instanceof Promise){

                        x.then(resolve,reject);

                        return;

                    }

                    resolve(x)

                }catch(err){

                    reject(x)

                }

            })

        });

    }

    catch(rejectedCallBack){

        return this.then(null,rejectedCallBack)

    }

}

module.exports = Promise;

then链的执行原理
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 本文旨在简单讲解一下javascript中的Promise对象的概念,特性与简单的使用方法。并在文末会附上一...
    _暮雨清秋_阅读 2,216评论 0 3
  • 在ES6当中添加了很多新的API其中很值得一提的当然少不了Promise,因为Promise的出现,很轻松的就给开...
    嘿_那个谁阅读 3,677评论 2 3
  • Promise 对象 Promise 的含义 Promise 是异步编程的一种解决方案,比传统的解决方案——回调函...
    neromous阅读 8,727评论 1 56
  • 一、Promise的含义 Promise在JavaScript语言中早有实现,ES6将其写进了语言标准,统一了用法...
    Alex灌汤猫阅读 837评论 0 2
  • ### 背景 Promise是异步编程的一种解决方案,它可以解决异步回调地狱的问题,防止层层嵌套对程序代码带来的难...
    小武song阅读 1,191评论 1 0