美文网首页
asnyc 解决异步问题

asnyc 解决异步问题

作者: Amy_yqh | 来源:发表于2019-08-17 14:14 被阅读0次
    function fn(){
        return new Promise((resolve)=>{
            setTimeout(()=>{
                resolve('hi')
            },1000)
        })
    }
    
    (async function(){
        let res1 =await fn();
        console.log('第一步')
    
        let res2 = await fn();
        console.log(res2+'第二步')
    
        let res3 = await fn();
        console.log(res3+'第三步')
    
        console.log('================')
    })()
    
    async function  bb(){
        let res1 =await fn();
        console.log('第一步')
    
        let res2 = await fn();
        console.log(res2+'第二步')
    
        let res3 = await fn();
        console.log(res3+'第三步')
    
        console.log('================')
    }
    // bb()
    
    let obj = {
        say:async function(){
           let result =  await fn()
           console.log('say:'+result)
        },
        eat: async ()=>{
            let result = await fn();
            console.log('eat:'+result)
        }
    }
    
    
    
    // awaint只能在async函数中执行
    // await fn()
    
    let  aa  = async function(){
        await obj.eat();
        await obj.say()
        console.log('```````````````````')
    }
    aa()
    

    相关文章

      网友评论

          本文标题:asnyc 解决异步问题

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