【发布时间】:2023-01-23 19:31:15
【问题描述】:
我在 TS playground 上有一个示例代码代表了我的问题。 在异步函数中,我在等待承诺后记录结果,但只记录承诺运行中的代码,而不是它之外的日志。有人可以解释这个问题吗?
这是代码:
const asyncFnc = async () => {
let result = false;
await new Promise(resolve => {
setTimeout(() => {
// This log worked
console.log('waited 5s');
result = true;
}, 5000);
});
// This log did not worked
console.log(result);
}
asyncFnc();
【问题讨论】:
标签: javascript async-await promise