function Promise(fn) {
var status = 'pending'
function resolve() {
status = 'resolve'
successArray.forEach(node => { node.apply(undefined, arguments) })
}
function reject() {
status = 'reject'
failArray.forEach(node => { node.apply(undefined, arguments) })
}
let [successArray,failArray] = [[],[]]
fn.call(undefined,resolve,reject)
return {
then: function (successFn,failFn){
successArray.push(successFn)
failArray.push(failFn)
return undefined
}
}
}
Promise(function(resolve,reject) {
setTimeout(function(){
resolve('成功')
},1000)
}).then((res)=>{
console.log(res)
})
网友评论