先执行test1(),再执行test2:
function test1() {
return new Promise(resolve => {
setTimeout(() => resolve("test1"), 2000);
});
}
function test2() {
console.log("test2");
}
async function test3() {
const result = await test1();
console.log(result);
test2();
}
test3();
console.log('end');
输出:
> end
> test1
> test2
js在线编辑器:
https://jsrun.net/new