7

I'm trying to log a statement in an async function as follows:

async generateCharts (insights) {
  const { url } = await this.reportsClient.createReport(insights)
  console.log('url from reports', url)
  return this.parse(url)
}

Log statement doesn't show up though, and I'm sure it's because of the async function. Is that correct? Anyway to work around this?

4

2 回答 2

4

您的示例缺少上下文,但恕我直言,那是因为您的 createReport 函数永远不会实现。没有其他原因无法执行 console.log。

于 2017-01-30T16:56:01.097 回答
4

请注意,错误在异步函数 中被“默默地”吞没——就像在普通的 Promises中一样。除非我们在 await 表达式周围添加 try / catch 块,否则未捕获的异常——无论它们是在你的 async 函数的主体中引发还是在 await 期间被挂起——都会拒绝 async 函数返回的承诺。

javascript-async-await#错误处理

于 2017-01-30T17:49:50.893 回答