js的Promise总结

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyPromise</title>
</head>
<body>
    <script>
        function MyPromise(excuter) {
            this.promiseState = 'pending'
            this.promiseValue = undefined
            this.callbacks = []
            resolve = result => {
                if(this.promiseState !== 'pending') return
                this.promiseState = 'resolved'
                this.promiseValue = result
                this.callbacks.forEach(fn => {
                    fn.onResolve(this.promiseValue)
                })

            }
            reject = error => {
                if(this.promiseState !== 'pending') return
                this.promiseState = 'rejected'
                this.promiseValue = error
                this.callbacks.forEach(fn => {
                    fn.onReject(this.promiseValue)
                })
                
            }
            try {
                excuter(resolve, reject)
            } catch (e) {
                reject(e)
            }
        }
        MyPromise.prototype.then = function(onResolve, onReject) {
            if(typeof onResolve !== 'function') {
                onResolve = value => value
            }
            if(typeof onReject !== 'function') {
                onReject = reason => {
                    throw reason
                }
            }
            const callback = type => {
                try {
                  const res = type(this.promiseValue)
                  if(res instanceof MyPromise) {
                    res.then(r => {
                        resolve(r)
                    }, error => {
                        reject(error)
                    })
                  } else {
                    resolve(res)
                  }

                } catch (e) {
                    reject(e)
                }
            }
            if(this.promiseState === 'resolved') {
                callback(onResolve)
                
            }
            if(this.promiseState === 'rejected') {
                callback(onReject)
                
            }
            if(this.promiseState === 'pending') {
                this.callbacks.push({
                    onResolve: function() {
                        callback(onResolve)
                    },
                    onReject: function() {
                        callback(onReject)
                    }
                })
            }
        }
        MyPromise.prototype.catch = function(onReject) {
            return this.then(null, onReject)
        }
        function f() {
            return new MyPromise((resolve, reject) => {
                setTimeout(() => {
                    resolve(23)
                }, 3000)
            })
        }
        f().then(val => {
            console.log(val)
            return new MyPromise((resolve, reject) => {
                setTimeout(() => {
                    console.log('123', val + 5)
                    resolve(val + 5)
                }, 7000)
            })
        }).then(val1 => {
            console.log(val1)
        })
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值