自己封装promise

function Promise(fn) {
    console.log(3);
    //每一次执行Promis直接设置为pending
    this.PromiseStatus = 'pending'
    this.PromiseRsult = undefined
    //异步操作里保存要执行的函数
    this.callBack = []
    //让里面的函数指向Promise
    let that = this
    function resolve(res) {
        //console.log(res);
        // console.log(2);
        //如果此时PromiseStatus等于pending,会执行return直接 强制 让函数结束
        if (that.PromiseStatus !== 'pending') return
        //修改对象状态
        that.PromiseStatus = 'fulfilled'
        //获得对象的值
        that.PromiseRsult = res
        that.callBack.map(val => {
            //console.log(val);
            val.onResolve(res)

        })
    }
    function reject(res) {
        if (that.PromiseStatus !== 'pending') return
        //修改对象状态
        that.PromiseStatus = 'rejected'
        //获得对象的值
        that.PromiseRsult = res
        that.callBack.map(val => {
            val.onReject(res)
        })
    }
    try {
        //console.log(fn);
        fn(resolve, reject)
    } catch (error) {
        reject(error)
    }
}

//封装then
Promise.prototype.then = function (onResolve, onReject) {
    console.log(4);
    if (typeof onReject !== Function) {
        onReject = (e) => {
            throw e
        }
    }
    return new Promise((resolve, reject) => {
        if (this.PromiseStatus == 'fulfilled') {
            try {
                let result = onResolve(this.PromiseRsult)
                if (result instanceof Promise) {
                    result.then(r => {
                        resolve(r)
                    }, s => {
                        reject(s)
                    })
                } else {
                    resolve(result)
                }
            } catch (error) {
                reject(error)
            }

        }
        if (this.PromiseStatus == 'rejected') {
            try {
                let result = onReject(this.PromiseRsult)
                if (result instanceof Promise) {
                    result.then(r => {
                        resolve(r)
                    }, s => {
                        reject(s)
                    })
                } else {
                    resolve(result)
                }
            } catch (error) {
                reject(error)
            }

        }
        if (this.PromiseStatus == 'pending') {
            let that = this
            this.callBack.push({
                onResolve: function () {
                    try {
                        let result = onResolve(that.PromiseRsult)
                        // console.log(result);
                        if (result instanceof Promise) {
                            result.then(r => {
                                resolve(r)
                            }, s => {
                                reject(s)
                            })
                        } else {
                            resolve(result)
                        }
                    } catch (error) {
                        reject(error)
                    }

                },
                onReject: function () {
                    try {
                        let result = onReject(that.PromiseRsult)
                        if (result instanceof Promise) {
                            result.then(r => {
                                resolve(r)
                            }, s => {
                                reject(s)
                            })
                        } else {
                            resolve(result)
                        }
                    } catch (error) {
                        reject(error)
                    }

                }
            })
        }
    })
}

//封装catch
Promise.prototype.catch = function (onReject) {
    return this.then(undefined, onReject)
}

html;
 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="./promise1.js"></script>
    <title>Document</title>
</head>

<body>

    <div>如果promise中执行的是异步操作,那么先执行then,再执行promise中的操作。如果不是,那么则先执行promise中的操作,最后再执行then中操作</div>

    <script>
        let p = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('OK')
            }, 1000)
            //throw 'erro'
            // resolve('OK')
            // reject('erro')
            //console.log(1);
        })

        p.then(resolve => {
            console.log(2);

        }).then(resolve => {

            console.log(resolve);

        })

        console.log(p);
        // console.log(p);
        // let res = p.then(resolve => {

        //     console.log(111);
        // })

        // console.log(res);



    </script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值