reactiverse / es4x

🚀 fast JavaScript 4 Eclipse Vert.x
https://reactiverse.io/es4x/
Apache License 2.0
877 stars 75 forks source link

Problem using await #585

Closed tpfau closed 2 years ago

tpfau commented 2 years ago

Hi, I'm currently having a problem (likely me not understanding Javascript again) with the await keyword in es4x code. If I take the example from here:

try {
  let server = await vertx
    .createHttpServer()
    .listen(0);

  console.log('Server Ready!');
} catch (err) {
  console.log('Server startup failed!')
}

and simply put this into my index.js and try to run it with es4x index.js, it fails with:

Failed in deploying verticle caused by SyntaxError: /home/thomas/Work/TestJs2/index.js:5:21 Expected ; but found vertx
  let server = await vertx

As I said, likely me screwing up javascript again, but a hint would be great.

pmlopes commented 2 years ago

Hi @tpfau I believe the issue is that you're trying to use await outside an async function.

If you would wrap your script as:

(async function futureTest1 () {
  let server = await vertx
      .createHttpServer()
      .requestHandler(req => {
      })
      .listen(0);

  console.log('Server Ready!');
})();

I think it would work. The error message is comming from graal and indeed is not very descriptive

tpfau commented 2 years ago

Thanks, That was indeed the issue. Might be good to put the example in an async function then. Should I just put in a PR with your suggestion as an update for the docs?

pmlopes commented 2 years ago

Please do! Thanks!