promise链式调用与非链式调用方法

非链式调用promise

const PENDING = ‘pending’
const FULFILLED = ‘fulfilled’
const REJECTRD = ‘rejected’

function Promise1 (executor) {
let _this = this
this.state = PENDING
this.value = undefined // 成功原因
this.reason = undefined // 失败原因
this.onFulfilled = [] // 成功的回调
this.onRejected = [] // 失败的回调
function resolve (value) {
console.log(‘resolve’)
if (_this.state === PENDING) {
_this.state = FULFILLED
_this.value = value
console.log(_this.onFulfilled)
_this.onFulfilled.forEach(fn => fn(value))
}
}
function reject (reason) {
console.log(‘reject’)
if (_this.state === PENDING) {
_this.state = REJECTRD
_this.reason = reason
_this.onRejected.forEach(fn => fn(reason))
}
}

try {
    executor(resolve, reject)
} catch (e) {
    reject(e)
}

}

Promise1.prototype.then = function (onFulfilled, onRejected) {
console.log(‘then-onFulfilled’, onFulfilled)
console.log(‘then-onRejected’, onRejected)
if (this.state === FULFILLED && typeof onFulfilled === ‘function’) {
onFulfilled(this.value)
}
if (this.state === REJECTRD && typeof onFulfilled === ‘function’) {
onRejected(this.value)
}
if (this.state === PENDING) {
typeof onFulfilled === ‘function’ && this.onFulfilled.push(onFulfilled)
typeof onRejected === ‘function’ && this.onRejected.push(onRejected)
}
}

var p = new Promise1((resolve, reject)=>{
setTimeout(() => {
resolve(4)
}, 1000)
})
p.then((res)=>{
console.log(res, ‘res’)
})
p.then((res1)=>{
console.log(res1, ‘res1’)
})

链式调用

测试
npm install -g promises-aplus-tests
promises-aplus-tests promise.js(文件名)
promises-aplus-tests 中共有 872 条测试用例。以上代码,可以完美通过所有用例。

const PENDING = ‘pending’
const FULFILLED = ‘fulfilled’
const REJECTRD = ‘rejected’

function Promise (executor) {
let _this = this
this.state = PENDING
this.value = undefined // 成功原因
this.reason = undefined // 失败原因
this.onFulfilled = [] // 成功的回调
this.onRejected = [] // 失败的回调
function resolve (value) {
if (_this.state === PENDING) {
_this.state = FULFILLED
_this.value = value
_this.onFulfilled.forEach(fn => fn(value))
}
}
function reject (reason) {
if (_this.state === PENDING) {
_this.state = REJECTRD
_this.reason = reason
_this.onRejected.forEach(fn => fn(reason))
}
}

try {
    executor(resolve, reject)
} catch (e) {
    reject(e)
}

}

Promise.prototype.then = function (onFulfilled, onRejected) {
let _this = this
onFulfilled = typeof onFulfilled === ‘function’ ? onFulfilled : value => value
onRejected = typeof onRejected === ‘function’ ? onRejected : reson => { throw reson }

let promise2 = new Promise((resolve, reject) => {
    if (_this.state === FULFILLED) {
        setTimeout(() => {
            try {
                let x = onFulfilled(_this.value)
                resolvePromise1(promise2, x, resolve, reject)
            } catch (error) {
                reject(error)
            }
        })
    } else if (_this.state === REJECTRD) {
        setTimeout(() => {
            try {
                let x = onRejected(_this.reason)
                resolvePromise1(promise2, x, resolve, reject)
            } catch (error) {
                reject(error)
            }
        })
    } else if (_this.state === PENDING) {
        _this.onFulfilled.push(() => {
            setTimeout(() => {
                try {
                    let x = onFulfilled(_this.value)
                    resolvePromise1(promise2, x, resolve, reject)
                } catch (error) {
                    reject(error)
                }
            })
        })
        _this.onRejected.push(() => {
            setTimeout(() => {
                try {
                    let x = onRejected(_this.reason)
                    resolvePromise1(promise2, x, resolve, reject)
                } catch (error) {
                    reject(error)
                }
            })
        })
    }

})
return promise2

}

function resolvePromise1 (promise2, x, resolve, reject) {
if (promise2 === x) {
reject(new TypeError(‘chanining cycle’))
}
if (x !== null && (typeof x === ‘object’ || typeof x === ‘function’)) {
// 函数和对象
let used;
try {
let then = x.then
if (typeof then === ‘function’) {
then.call(x, (y) => {
if (used) return
used = true
resolvePromise1(promise2, y, resolve, reject)
}, ® => {
if (used) return
used = true
reject®
})
} else {
if (used) return
used = true
resolve(x)
}
} catch (error) {
if (used) return
used = true
reject(error)
}

} else {
    // 普通值
    resolve(x)
}

}

// 测试使用

Promise.defer = Promise.deferred = function () {
let dfd = {};
dfd.promise = new Promise((resolve,reject)=>{
dfd.resolve = resolve;
dfd.reject = reject;
})
return dfd;
}

module.exports = Promise;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值