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();
更多专业前端知识,请上 【猿2048】www.mk2048.com