google / traceur-compiler

Traceur is a JavaScript.next-to-JavaScript-of-today compiler
Apache License 2.0
8.17k stars 578 forks source link

Async/Await freezing when running SQL #2074

Open jadencarver opened 8 years ago

jadencarver commented 8 years ago

I'm having an issue getting predictable behavior out of async/await when running SQL: https://github.com/jadencarver/sean_app - I tested that it functions as expected when the Promise is a simple setTimeout, but the await fails to resume when the Promise is bound to a SQL query. I don't believe I did anything wrong implementing the Promise.

What could cause this inconsistent behavior? I understand that await/async is experimental, but I feel obligated to report this issue as has been a barrier for me.

arv commented 8 years ago

I can't see anything wrong with your code or the generated code:

http://google.github.io/traceur-compiler/demo/repl.html#%2F%2F%20Options%3A%20--async-functions%20--modules%3Dcommonjs%0A%0Alet%20routes%20%3D%20require(%22express%22)%3B%0Alet%20postgresql%20%3D%20require('connect-pgclient')%3B%0Alet%20morgan%20%3D%20require('morgan')%3B%0A%0Alet%20accessDatabase%20%3D%20postgresql(%7B%0A%20%20%20%20config%3A%20%7B%0A%20%20%20%20%20%20database%3A%20%22sean_app_dev%22%2C%0A%20%20%20%20%7D%2C%0A%20%20%20%20transaction%20%3A%20true%0A%20%20%7D)%3B%0A%0Aroutes()%0A%20%20.use(morgan('combined'))%0A%20%20.get(%22%2F%22%2C%20accessDatabase%2C%20async%20(req%2C%20res)%20%3D%3E%20%7B%0A%20%20%20%20let%20sleep%20%3D%20(ms%20%3D%200)%20%3D%3E%20%7B%0A%20%20%20%20%20%20return%20new%20Promise(r%20%3D%3E%20setTimeout(r%2C%20ms))%3B%0A%20%20%20%20%7D%3B%0A%20%20%20%20let%20sql%20%3D%20(sql)%20%3D%3E%20%7B%0A%20%20%20%20%20%20return%20new%20Promise((resolve%2C%20reject)%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20req.db.client.query(sql%2C%20(err%2C%20result)%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20if%20(err)%20%7B%20reject(err)%20%7D%0A%20%20%20%20%20%20%20%20%20%20else%20%7B%20resolve(result)%20%7D%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20console.log(%22start%22)%3B%0A%20%20%20%20await%20sleep(1000)%3B%0A%20%20%20%20console.log(%22wake%20up%22)%3B%0A%20%20%20%20%2F%2F%20sql(%22SELECT%20*%20FROM%20users%22).then(result%20%3D%3E%20%7B%0A%20%20%20%20%2F%2F%20%20%20console.log(%22.then%20works%3A%20%22%2C%20result.rows)%3B%0A%20%20%20%20%2F%2F%20%20%20res.send(result.rows)%3B%0A%20%20%20%20%2F%2F%20%7D)%0A%20%20%20%20%2F%2F%20TODO%3A%20FOR%20SOME%20REASON%20THIS%20AWAIT%20DOESN'T%20WORK%2C%20EVEN%20THOUGH%20THE%20PROMISE%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20ABOVE%20WORKS!%0A%20%20%20%20result%20%3D%20await%20sql(%22SELECT%20*%20FROM%20users%22)%3B%0A%20%20%20%20console.log(%22await%20works%22)%3B%0A%20%20%20%20res.send(result.rows)%3B%0A%20%20%20%20console.log(%22done%22)%3B%0A%20%20%7D)%0A%20%20.listen(3000)%3B