Promise

2018-07-26  本文已影响0人  Lucien_d70a
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)
})
上一篇下一篇

猜你喜欢

热点阅读