async,await

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();

原文地址:https://www.cnblogs.com/lwwen/p/7338415.html