denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.75k stars 2.55k forks source link

Await is only valid in async function #172

Closed Wamzel closed 3 years ago

pinage404 commented 3 years ago

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 :

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

denysdovhan commented 3 years ago

This is more like a question. A top-level await is coming.