// 2s 之后返回双倍的值 function doubleAfter2seconds(num) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(2 * num) }, 2000); } ) }
async function testResult() { let first = await doubleAfter2seconds(30); #2s 之后返回双倍的值 let second = await doubleAfter2seconds(50); #2s 之后返回双倍的值 let third = await doubleAfter2seconds(30); #2s 之后返回双倍的值 console.log(first + second + third); }
console.log("我在异步函数后面,会先执行谁呢");
1、先:我在异步函数后面,会先执行谁呢
2、后:6秒后,控制台输出220, 我们可以看到,不影响后面的函数执行。
await只能用在async里面