等待后顺序执行异步代码,而不是立即显示控制台输出

Asynchronous code executing sequentially after wait instead of showing console output immediately

这是我的示例代码:

async function counter() {
    console.log('1');

    let next = new Promise((resolve) => {setTimeout(() => resolve(3), 5000)});

    let three = await next;

    console.log('2');
    console.log(three)
}

counter();

this question 上接受的答案是

When you execute something synchronously, you wait for it to finish before moving on to another task. When you execute something asynchronously, you can move on to another task before it finishes.

据我了解,2 应该立即记录到控制台,因为该函数是异步的,它不应该等待另一个任务(承诺)完成(解决)。

但是,我看到 23 在等待 5 秒后一起记录在控制台中。这不是同步函数根据接受的答案中的上述文本执行的操作吗?

谢谢。

The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise.

您可以阅读更多关于 Await Here

你可以阅读更多关于

如果您在 async 函数中使用 await,基本上会发生什么,它会导致等待直到 promiseresolved。但在该函数之外仍将 运行 与此函数并行。