Skip to content

Await is only valid in async function #184

Answered by pinage404
Wamzel asked this question in WTF Questions
Discussion options

You must be logged in to vote

await is syntax sugar around Promise


There is two cases to use await :

  1. outside of any function / at top level
  2. in a function

For the first case, the specification is currently in progress and will come in the future


For the second case, a function that call a Promise must :

  • give a callback that will be executed after the Promise finished the asynchronous task
  • let the Promise do side-effect
  • return a Promise

If you care about the result of the Promise and want to wait before executing others instructions, that means that this function will wait for an asynchronous task, and be asynchronous as well, that why the async keyword is required

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by denysdovhan
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #172 on February 05, 2021 14:47.