function Pro(callback) {
const pending = 'pending'
const fulfilled = 'fullfilled'
const rejected = 'rejected'
this.state = pending
this.value = null
this.reason = null
this.fullfiledCallback = []
this.rejectedCallback = []
this.resolve = data => {
setTimeout(() => {
if (this.state == pending) {
this.state = fulfilled
this.value = data
this.fullfiledCallback.map(fn => fn(this.value))
}
})
}
this.reject = reason => {
setTimeout(() => {
if (this.state == pending) {
this.state = rejected
this.reason = reason
this.rejectedCallback.map(fn => fn(this.reason))
}
})
}
this.then = function (successFn, errorFn) {
this.fullfiledCallback.push(successFn)
this.rejectedCallback.push(errorFn)
}
this.catch = (errorFn) => {
this.rejectedCallback.push(errorFn)
}
callback(this.resolve, this.reject)
}
new Pro((resolve,reject)=>{
setTimeout(()=>{resolve([1,2,3]);},1000)
}).then((data)=>{
console.log(data);
},(error)=>{
console.log(error);
})
console