async and await in node js


async and await are modern JavaScript features that simplify working with asynchronous code, making it look and behave more like synchronous code. These features are built on top of Promises and provide a more readable and concise way to handle asynchronous operations. Here's a detailed explanation:

1. What is async?

The async keyword is used to declare an asynchronous function. An asynchronous function always returns a Promise, and the result of the function will be wrapped in a Promise if it is not already a Promise.

Syntax:

async function myFunction() { // Asynchronous code }

2. What is await?

The await keyword can only be used inside an async function. It pauses the execution of the async function until the Promise is resolved or rejected, and then resumes execution with the resolved value or throws the rejected error.

Syntax:

const result = await somePromise;
  • If somePromise resolves, result will be assigned the resolved value.
  • If somePromise rejects, the await expression will throw an error, which can be caught with a try/catch block.

3. Example of async and await

Here’s a basic example demonstrating how async and await work:

const fs = require('fs').promises; // Define an async function async function readFile() { try { // Use await to pause execution until the promise is resolved const data = await fs.readFile('example.txt', 'utf8'); console.log(data); // Output the file contents } catch (error) { console.error('Error reading file:', error); } } // Call the async function readFile();

In this example:

  • fs.readFile returns a Promise.
  • The await keyword is used to wait for the Promise to resolve and get the file contents.
  • If an error occurs, it’s caught with a try/catch block.

4. Handling Multiple Promises

You can use await to handle multiple Promises concurrently by using Promise.all():

const fs = require('fs').promises; async function readFiles() { try { const [file1, file2] = await Promise.all([ fs.readFile('file1.txt', 'utf8'), fs.readFile('file2.txt', 'utf8') ]); console.log(file1); console.log(file2); } catch (error) { console.error('Error reading files:', error); } } readFiles();

In this example:

  • Promise.all() is used to run both file reads in parallel.
  • await is used to wait until both Promises are resolved.

5. Sequential Execution

If you need to perform asynchronous tasks sequentially, you can use await in sequence:

const fs = require('fs').promises; async function processFiles() { try { const file1 = await fs.readFile('file1.txt', 'utf8'); console.log('File 1:', file1); const file2 = await fs.readFile('file2.txt', 'utf8'); console.log('File 2:', file2); } catch (error) { console.error('Error:', error); } } processFiles();

In this example:

  • File reads are performed one after the other, with each operation waiting for the previous one to complete.

6. Error Handling

Error handling with async and await is done using try/catch blocks:

async function fetchData() { try { const response = await fetch('https://api.example.com/data'); const data = await response.json(); console.log(data); } catch (error) { console.error('Error fetching data:', error); } } fetchData();

In this example:

  • If the fetch operation or the json() method throws an error, it’s caught by the catch block.

7. Key Points

  • async Functions: Always return a Promise. If the function returns a value, that value is wrapped in a Promise.
  • await Keyword: Pauses the execution of an async function until the Promise resolves, and resumes execution with the resolved value.
  • Error Handling: Use try/catch blocks within async functions to handle errors.

Summary

  • async/await: Simplify working with asynchronous code, making it look more like synchronous code.
  • async Function: Returns a Promise.
  • await Keyword: Waits for a Promise to resolve or reject.
  • Error Handling: Use try/catch for error management.