How to use async/await in Node.js
Tags: node, javascript, express
Published: 2021-07-28 12:37:50

---
title: How to use async/await in Node.js
published: true
date: 2021-03-31 00:00:00 UTC
tags: node, javascript, express
canonical_url: https://www.mariokandut.com/how-to-use-async-await-in-node-javascript/
---

Async/Await can be used to write asynchronous code in Node.js that reads like synchronous code and is available in Node since v.7.6 and up (officially rolled out with Node v8 and ECMAScript 2017). Any code that uses Promises can be converted to use async/await. For an overview of promises in Node.js have a look at the article: [Promises in Node.js](https://www.mariokandut.com/understanding-promises-in-node-js/)

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#what-is-asyncawait)What is async/await?

The basic idea behind `async/await` is to write async code that behaves similarly to synchronous code, it should execute in the order it was written.

Let's look at an example to compare code execution with `Promises` and with `async/await`:

Create or add a project folder.

```
mkdir node-async-await
```

Initialize project with `npm init -y` to be able to install node packages.

```
cd node-async-await
npm init -y
```

Install `node-fetch` to make fetch requests.

```
npm install node-fetch
```

Create an `index.js` file.

```
touch index.js
```

Copy example code:

```
const fetch = require('node-fetch');

async function run() {
  const status = await fetch(
    'https://jsonplaceholder.typicode.com/todos/1',
  )
    .then(response => response.json())
    .then(res => res);
  console.log(status, 'Complete!');
}
run();
```

We need a second file for the comparison code for Promises.

```
touch index-promise.js
```

Copy the example code:

```
const fetch = require('node-fetch');

const data = fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(res => res);
console.log(data, 'Complete!');
```

Now execute both files.

With `async/await` code execution inside the run function is paused without blocking the Node.js process until the Promise chain resolves. Without `async/await` the `console.log` statement will run before the Promise has resolved. The output in the command line will look like this `Promise { <pending> } Complete!`.

If we want the same behaviour as the `async/await` with just `Promises` the `console.log` has to be nested to ensure data was present when using it. With `async/await` we don't have to nest the code, so it is more readable.

**Async functions will implicitly return a Promise, which resolves with the return value from the function.** Using `await` will pause execution in the async function, until the awaited Promise resolves. Using await doesn't block the main thread like a synchronous operation would, but pauses execution within the async function where it is used. Once the Promise resolves, execution will begin again where it left off.

The `await` keyword can only be used inside an `async` function. It can't be used in the global context and must enclose it in an async function.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#error-handling-with-asyncawait)Error Handling with async/await

Error handling in `async/await` comes with a big improvement. When an error or an exception is thrown, it will reference the origin of the error (the function it originated from). Hence, the context of the call is not lost, and the stack trace can be used to debug easier.

Error are handled by wrapping the `await` calls in a `try/catch` block, instead of using `.catch`.

Let's add error handling to the example from above:

```
const fetch = require('node-fetch');

async function run() {
  try {
    const status = await fetch(
      'https://jsonplaceholder.typicode.com/todos/1',
    )
      .then(response => response.json())
      .then(res => res);
    console.log(status, 'Complete!');
  } catch (error) {
    console.log(error);
  }
}
run();
```

An `async` function return a `Promise` implicitly, so handling errors with a catch handler would also be possible. Though handling errors with a `try/catch` block is the common way of doing it, so the error handling is not delegated to the outside of async function.

```
const fetch = require('node-fetch');

async function run() {
  const status = await fetch(
    'https://jsonplaceholder.typicode.com/todos/1',
  )
    .then(response => response.json())
    .then(res => res);
  console.log(status, 'Complete!');
}

run.catch(error => console.log(error));
```

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- `async/await` is the modern standard for asynchronous code in Node.js
- Using the async and await keywords can further simplify code and remove nested Promises.
- Asynchronous code looks and behaves similar to synchronous code without blocking the Node.js Event Loop.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#references-and-big-thanks)References (and Big thanks):

[HeyNode](https://heynode.com/tutorial/use-asyncawait-promises/),[MDN](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await),[MDN Async Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)