简体   繁体   English

等待后的 console.log 不打印其内容

[英]console.log after await doesnt print its content

I read about async/await, but I've a question.我读到了异步/等待,但我有一个问题。 I know that await only works with promises, but why in the code below, the last console doesn't print at all!我知道 await 仅适用于 Promise,但为什么在下面的代码中,最后一个控制台根本不打印!

async function printMe() {
  console.log("First")
  await new Promise (resolve => {setTimeout(() => console.log("Async!!!"), 3000)})
  console.log("Last")
}

printMe()

When using Promise , you need to call the resolve method to fulfil the promise:使用Promise时,需要调用resolve方法来实现 promise:

 async function printMe() { console.log("First") await new Promise (resolve => {setTimeout(() => { console.log("Async,.!") resolve() }, 3000)}) console.log("Last") } printMe()

NB: There is also a reject method available, you can find more about it by reading the related MDN page注意:还有一种reject方法可用,您可以通过阅读相关的 MDN 页面找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM