async/await in Node

noun

say it: “AY-sink uh-WAYT in NOHD” /ˈeɪsɪŋk əˈweɪt ɪn ˈnoʊd/

A way of writing code that waits for slow work, such as reading a file or the database, so it reads top to bottom.

What does async/await in Node do?

async/await writes promise-based code that reads top to bottom.

app.get('/recipes/:id', async (req, res) => {
  const recipe = await db.findRecipe(req.params.id);
  recipe ? res.json(recipe) : res.sendStatus(404);
});

In a sentence

  • “The route uses async/await to wait for the database before answering.”
  • “With async/await in Node, a failed database call can be caught with try/catch.”

Level: Journeyman · More Node.js terms at this level

Updated