举个例子
function resolveAfter1Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve(‘resolved1‘);
}, 5000);
});
}
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve(‘resolved2‘);
}, 10000);
});
}
async function asyncCall() {
console.log(‘calling‘);
const result = await resolveAfter2Seconds();
console.log(result);//等待10秒后执行这句话
console.log(‘calling‘);//接着执行这句话
const result2 = await resolveAfter1Seconds();
console.log(result2);//再等五秒后执行这句话
// expected output: "resolved"
}
asyncCall();
原文:https://www.cnblogs.com/attack204/p/14638615.html