Skip to content
Advertisement

await is only valid in async function

I wrote this code in lib/helper.js:

JavaScript

Then I tried to use it in another file :

JavaScript

I got an error:

JavaScript

What is the issue?

Advertisement

Answer

The error is not refering to myfunction but to start.

JavaScript

JavaScript


I use the opportunity of this question to advise you about an known anti pattern using await which is : return await.


WRONG

JavaScript

CORRECT

JavaScript

Also, know that there is a special case where return await is correct and important : (using try/catch)

Are there performance concerns with `return await`?

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement