class Qromise {
constructor(fn) {
this.status = 'pending'
this.error = ''
this.data = ''
try{
fn(this.onFullfilled.bind(this), this.onRejected.bind(this))
}catch(e){
this.reject(e)
}
}
then(fn) {
if( this.status === 'Fullfilled' ) {
fn.call(this, this.data)
}
return this
}
reject(fn) {
if( this.status === 'Rejected' ) {
fn.call(this, this.error)
}
return this
}
onFullfilled(data) {
console.log('on fullfilled')
this.data = data
this.status = 'Fullfilled'
}
onRejected(err) {
console.log('on rejected')
this.error = err
this.status = 'Rejected'
}
}
module.exports = Qromise
javascript 手写一个promise
猜你喜欢
转载自blog.csdn.net/qq_31687021/article/details/89642496
今日推荐
周排行