Promise

作者: Lucien_d70a | 来源:发表于2018-07-26 20:48 被阅读0次
    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)
    })
    

    相关文章

      网友评论

          本文标题:Promise

          本文链接:https://www.haomeiwen.com/subject/uctcfftx.html