Console.log doesn't work in async function
Asked Answered
S

2

15

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?

Sandpit answered 30/1, 2017 at 16:52 Comment(3)
I just ran your code and it works fine. Have you tried forcing createReport to fulfill to verify this?Isabeau
FYI, async functions are not part of ES6 (ES2015). They will part of thies year's release, ES2017.Playground
@MikeC looks like that would be the issue!Sandpit
M
4

Your example lacks of context but, IMHO, that's because your createReport function never fulfills. There are no other reasons why the console.log would not get executed.

Mn answered 30/1, 2017 at 16:56 Comment(1)
Same here. I had two functions with the same name. The second one took priority while I was editing the first one.Discriminator
K
6

Note that errors are swallowed "silently" within an async function – just like inside normal Promises. Unless we add try / catch blocks around await expressions, uncaught exceptions – regardless of whether they were raised in the body of your async function or while its suspended during await – will reject the promise returned by the async function.

javascript-async-await#error-handling

Kaikaia answered 30/1, 2017 at 17:49 Comment(0)
M
4

Your example lacks of context but, IMHO, that's because your createReport function never fulfills. There are no other reasons why the console.log would not get executed.

Mn answered 30/1, 2017 at 16:56 Comment(1)
Same here. I had two functions with the same name. The second one took priority while I was editing the first one.Discriminator

© 2022 - 2024 — McMap. All rights reserved.