DEV Community

Mario
Mario

Posted on • Originally published at mariokandut.com on

How to use async/await in Node.js

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

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
Enter fullscreen mode Exit fullscreen mode

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

cd node-async-await
npm init -y
Enter fullscreen mode Exit fullscreen mode

Install node-fetch to make fetch requests.

npm install node-fetch
Enter fullscreen mode Exit fullscreen mode

Create an index.js file.

touch index.js
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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

touch index-promise.js
Enter fullscreen mode Exit fullscreen mode

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!');
Enter fullscreen mode Exit fullscreen mode

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.

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();
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

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.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

HeyNode,MDN,MDN Async Function

Top comments (0)