Synchronous and Asynchronous Tasks in Node js

“Welcome to the dynamic realm of Node.js! If you’ve ever wondered about Synchronous and Asynchronous tasks in Node.js, you’re in the right place. 🚀 In this beginner-friendly guide, we’ll unravel these concepts step by step, making them easy to grasp even if you’re new to programming. Imagine having the ability to control the flow of your code or make your applications faster and more responsive—well, that’s exactly what understanding Synchronous and Asynchronous tasks can do for you! So, grab your favorite beverage, settle in, and let’s embark on this exciting Node.js journey together!” 🌟

What are Synchronous and Asynchronous Tasks in Node.js?

Imagine you’re in a restaurant waiting for your food. 🍔 Synchronous tasks in Node.js are like standing in a line, where each order is taken and fulfilled one after the other. So, you wait for your burger before the next person’s order is even taken.

On the other hand, Asynchronous tasks are more like placing a food delivery order. 🛵 You can place your order and then go about doing other things while you wait for your meal to arrive. Your order is being processed in the background, and you don’t have to wait around in line.

Synchronous task Example

Result:

Explanation:

In this example, we have a simple Node.js script that demonstrates a Synchronous task using fs.readFileSync().

  1. Before: We start by logging “Before” to the console, indicating the beginning of our script.
  2. Synchronous Task: Next, we use fs.readFileSync('./sometext.txt', "utf-8") to read the contents of a file named “sometext.txt” synchronously. This means that the script will pause execution at this line until the entire file is read and its contents are stored in the fileDataSync variable.
  3. Logging File Data: Once the file is read, we log the contents of fileDataSync to the console using console.log("fileDataSync => ", fileDataSync).
  4. After: Finally, we log “After” to the console, indicating the end of our script.

Asynchronous task Example

Result

Explanation:

In this example, we have a Node.js script that demonstrates an Asynchronous task using fs.readFile().

  1. Before: We start by logging “Before” to the console, indicating the beginning of our script.
  2. Asynchronous Task: Next, we use fs.readFile('./sometext.txt', "utf-8", (err, fileDataAsync) => {...}) to read the contents of a file named “sometext.txt” asynchronously. This means that the script will continue executing without waiting for the file reading operation to complete.
  3. Callback Function: The third argument of fs.readFile() is a callback function (err, fileDataAsync) => {...}. This function will be called once the file reading operation is complete. It takes two parameters: err for any errors that may occur, and fileDataAsync for the data read from the file.
  4. Logging File Data: Inside the callback function, we log the contents of fileDataAsync to the console using console.log("fileDataAsync => ", fileDataAsync).
  5. After: Finally, we log “After” to the console, indicating the end of our script.

Leave a Comment