asnyc 解决异步问题

2019-08-17  本文已影响0人  Amy_yqh
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()
上一篇下一篇

猜你喜欢

热点阅读