promise

将下列异步函数改编成promise

     <script>
            function fn(resolve,reject){
        setTimeout(function(){
            console.log(‘3秒后执行‘)
        },3000)
    }
    fn()
    console.log(‘立即执行‘)
    </script>

改编后的代码:

<script>
    let p =    new Promise(
            function fn(resolve,reject){
        setTimeout(function(){
            resolve()
        },3000)
    }
        )

    p.then(function(){
        console.log(‘3秒后执行‘)
    })
    console.log(‘立即执行‘)
    </script>

相关推荐