Tricky async declarations

github logo ・1 min read

Imagine the following code, using Express and Sequelize:

app.get('/', async (_, res) => {
  const result = await sequelizeModel.getStuff()[0]
  return res.status(200).send(result)

In simple words: given an endpoint, it will execute a query on the database and return the result.

But there's a small gotcha: that code works but doesn't return the results, because at the time of the promise creation, the [0] don't exist.

Only after the promise/async is fulfilled, the Sequelize object exists.

Working code:

app.get('/', async (_, res) => {
  const result = await sequelizeModel.getStuff()
  return res.status(200).send(result[0])
twitter logo DISCUSS
Classic DEV Post from May 30 '19

Which is the Best Static Site Generator and Why?

Best static sites generator and reasons

Jota Feldmann profile image
I'm J, a remote web developer, coding since 1998. Polyglot microservices stacks: Javascript/Node (proficient), Python, Java, and Go, sometimes as front-end (mostly Vue and some React).