代码改变世界

async,await

2017-08-10 11:37  改吧  阅读(270)  评论(0编辑  收藏  举报
function timeout(ms){
    return new Promise(function(resolve){
        setTimeout(resolve,ms)
    })
}

async function asyncPrint(value,ms){
    await timeout(ms);
    console.log(value)
}
asyncPrint('hello world',5000)

这段代码是过了5秒再显示hello world

await是要等待这句代码执行完,再执行下面的代码

 

 

async function f(){
    return 'hello wld'
}
f().then(function(re){
    console.log(re);
});

async方法是返回Promise对象

then()里面的res数据是async方法里面的return值

.then(),.catch();