前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Promise简单实现

Promise简单实现

作者头像
flytam
发布2020-01-14 17:09:12
3720
发布2020-01-14 17:09:12
举报

按照自己理解实现了下,不完美。。待填坑。all和race有问题

代码语言:javascript
复制
class MyPro {
    constructor(func) {
        this.state = "pending"; // or full or rejected
        this.value = undefined;
        this.tasks = [];
        this.errortasks = [];
        this.finnalytasks = [];
        function resolve(value) {
            if (this.state === "pending") {
                this.value = value;
                this.state = "full";
                this._gg(this.value);
                this
                    .tasks
                    .forEach(func => {
                        setTimeout(() => {
                            this.value = func(this.value)
                        })
                    });
            } else {
                return
            }

        }
        function reject(reason) {
            if (this.state === "pending") {
                this.state = "rejected";
                this
                    .errortasks
                    .forEach(func => {

                        setTimeout(() => {
                            if (typeof func === "function") {
                                func(reason)
                            }

                        })
                    });
            } else {
                return
            }
        }
        try {
            setTimeout(()=>func(resolve.bind(this), reject.bind(this)))
        } catch (e) {
            reject(e);
        } finally {
            setTimeout(() => {
                this
                    .finnalytasks
                    .forEach(func => setTimeout(() => {
                        func()
                    }))
            })

        }
    };
    then(onFulfilled, onRejected) {
        if (this.state === "pending") {
            this
                .tasks
                .push(onFulfilled)
            this
                .errortasks
                .push(onRejected)
        }
        return this;
    }; 
    catch (rejectFunc) {
        if (this.state === "pending") {
            this
                .errortasks
                .push(rejectFunc)
        }
        return this;
    };
    _gg(v){
        //提供个观察者模式接口

    }
    static resolve(val){
        return new MyPro((rel) =>rel(val))
    }
    static reject(){}
    static race(arr){
        return new MyPro((rel) =>{
            //当某一个触发就触发

            arr.forEach(item => {
                item._gg = (v) => rel(v)
            });

        })
    }
    static all(arr){
        let ps = arr.length;
        const result = [];
        return new MyPro((rel) =>{
            arr.forEach(item => {
                item._gg = (v) => {

                }
            })
            rel(result);
        })
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档