Node.js: Top 3 Assets of Asynchronous Programming

An Intro to Asynchronous Programming

Understanding the difference between synchronous and asynchronous is of paramount importance when it comes to grasping the concept of asynchronous programming in Node.js. 

So, let’s delve into these concepts:

Synchronous Code Execution:

In synchronous code execution, each line of code is executed one after the other in a sequential manner.

Synchronous operations block the execution of code until completion. So, if there’s a time-consuming task, the entire program will be halted until that task finishes.

Synchronous code is straightforward to reason since it follows a predictable flow, but it can lead to performance and responsiveness issues at times.

Example of synchronous code execution in Node.js:

console.log('Start');

console.log('Middle');

console.log('End');

In the above example, the statements will be executed in the exact order they appear: “Start,” “Middle,” and “End.” Each statement completes before the next one is executed.

Asynchronous Code Execution:

But, in asynchronous code execution, certain tasks are initiated and allowed to run independently without blocking the execution of other code.

Asynchronous operations do not wait for completion; instead, they immediately execute the next line of code.

Asynchronous operations typically involve I/O operations, network requests, file system operations, timers, and callbacks.

Example of asynchronous code execution in Node.js:

console.log('Start');
setTimeout(() => {
  console.log('Async task');
}, 2000);
console.log('End');

In the above example, the output will be:

Start
End
Async task

The statement “Start” is logged to the console.

The setTimeout function is called, which schedules the execution of the callback function after 2000 milliseconds (2 seconds).

While waiting for the 2-second timeout to complete, the program continues to execute the next line of code.

The statement “End” is logged to the console.

After the 2-second timeout, the callback function is executed, and “Async task” is logged to the console.

Hence, asynchronous code allows programs to initiate tasks, such as network requests or file system operations, and continue executing other code without waiting for those tasks to complete.

All in all: this non-blocking behavior is of paramount importance for building scalable and efficient applications.

In Node.js, the event loop and its underlying architecture enable asynchronous code execution and facilitate efficient handling of I/O operations, making it a suitable platform for developing highly performant and scalable applications.

Your partner: callbacks in Node.js

Leveraging callbacks and promises, developers can unlock the true potential of Node.js by handling concurrent operations and ensuring code execution remains non-blocking.

Let’s explore the fundamentals of working with callbacks and promises, along with best practices to avoid common pitfalls such as callback hell.

Callbacks come to be the traditional mechanism for handling asynchronous operations in Node.js. By passing a callback developers can define the behavior that should be executed once the operation is complete.

Example of working with callbacks in Node.js:

function fetchData(callback) {
  setTimeout(() => {
    const data = 'Some data';
    const error = null; // Simulating no error
    callback(error, data);
  }, 2000);
}
fetchData((err, data) => {
  if (err) {
    console.error('Error:', err);
  } else {
    console.log('Data:', data);
  }
});

Leave Room for Error Handling with Callbacks

Proper error handling is crucial when working with callbacks in Node.js. It’s common practice to pass an error object as the first argument to the callback function to indicate any issues during the asynchronous operation.

So, you should always check for errors and handle them appropriately, whether it’s logging an error message or triggering a specific action. Guarantee a great UX.

Example of error handling with callbacks in Node.js:

function fetchData(callback) {
  setTimeout(() => {
    const data = null; // Simulating an error
    const error = new Error('Data not found');
    callback(error, data);
  }, 2000);
}
fetchData((err, data) => {
  if (err) {
    console.error('Error:', err.message);
    // Perform additional error handling or fallback logic
  } else {
    console.log('Data:', data);
  }
});

Callback Hell and How to Avoid It

A.K.A. the pyramid of doom, is a situation where multiple nested callbacks make the code structure complex and hard to maintain.

Thereof, Asynchronous operations depending on each other can lead to deeply nested callbacks, reducing code readability and increasing the chances of errors.

Let’s avoid this, adopt techniques such as modularization, using named functions, or employing control flow libraries like Async.js or Promisify to flatten the callback structure. Mind future maintenance!

Promises

It represents the eventual completion or failure of an asynchronous operation, allowing you to chain multiple operations together and handle errors more effectively. They offer cleaner code syntax and help avoid the pyramid of doom.

Extra! Modern JavaScript features like async/await built upon promises, provide more intuitive and synchronous-looking syntax for handling asynchronous operations.

Example of working with promises in Node.js

function fetchData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const data = 'Some data';
      const error = null; // Simulating no error

      if (error) {
        reject(error);
      } else {
        resolve(data);
      }
    }, 2000);
  });
}

fetchData()
  .then((data) => {
    console.log('Data:', data);
  })
  .catch((err) => {
    console.error('Error:', err);
  });

When working with callbacks and promises in Node.js, best practices policy ensures clean and efficient code.

5 key practices include:

  1. Consistently handling errors and propagating them correctly through the callback or promise chain.
  2. Avoiding unnecessary nesting and flattening code structure.
  3. Properly cleaning up resources and handling memory leaks when working with callbacks.
  4. Leveraging utility libraries and nodejs frameworks that provide helpful functions for callback and promise handling.
  5. Staying consistent with coding conventions and choosing descriptive names for callback functions or promise variables.

Whether it’s callbacks for traditional asynchronous programming or promises with their enhanced readability, Node.js provides a rich ecosystem for handling operations, allowing developers to unlock the full potential of their applications.

Converting callback-based APIs to promise-based APIs

The 2022 promisify function uses the callback function as a benchmark and returns the promise. There are npm packages available to use so as to convert the callback function to promise.

Node.js gives you a npm-free function called promisify which you can use to convert the callback function to promise.

const promisify =
 (fn) =>
 (...args) =>
   new Promise((resolve, reject) => {
    fn(...args, (err, result) => {
     if (err) {
       reject(err);
     } else {
       resolve(result);
     }
   });
  });
Ta-da!
function add(a, b, callback) {
  setTimeout(() => callback(null, a + b), 100);
}

const addPromise = promisify(add);

const run = async () => {
  const first = await addPromise(1, 2);
  console.log(first); 
  const second = await addPromise(first, 3);
  console.log(second);
  const finalResult = await addPromise(second, 4);
console.log(finalResult);
};

Promise allows you to manage multiple asynchronous operations dependent of each other.

If you are using node.js version 8 or above, use util.promisify function to convert the callback function to promise.

const { promisify } = require("util");
const addPromise = promisify(add);

Working with async/await in Node.js

We invite you to watch the video and explore its benefits. Or else, you can visit this webpage so as to make it look like synchronous code using async/await:


You can also explore some patterns and functionalities, such as: retry with exponential backoff, multi-parallel request and intermediate values. 

Or even explore the try and catch in the use of promises to give a more synchronous-looking style to the code:

Any errors thrown by the promise can be caught using a surrounding try...catch block.

async function fetchData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const error = new Error('An error occurred');
      reject(error);
    }, 2000);
  });
}async function main() {
  try {
    const data = await fetchData();
    console.log('Data:', data);
  } catch (error) {
    console.error('Error:', error);
  }
}

Introduction to streams in Node.js

Streams allow developers to continuously interact with data from a source. Readable, writable, duplex, and transform are the four main types of streams in Node.js. Each stream is an eventEmitter instance within a time interval.

To access the node:stream module:

const stream = require('node:stream');

node:stream helps create new types of stream instances. Usually, the node:stream module is not necessary to consume streams in Node.js.

Working with readable and writable streams

A readable stream can receive data, but it cannot send it, A.K.A. piping. Data sent to the read stream is buffered until the consumer starts reading the data. fs.createReadStream() allows us to read the contents of the file.

Some examples are: process.stdin, fs read streams, HTTP responses on the client, HTTP requests on the nodejs server, etc.

A writable stream can send data, but it cannot receive data. fs.createWriteStream() allows us to write data to a file. Examples: HTTP requests on the client, HTTP responses on the server, fs write streams, process.stout, process.stderr etc.

Duplex are streams that implement both read and write streams within a single component. Used to achieve the read or write mechanism since it does not change the data. Example: TCP socket connection (net.Socket).

Transform streams are similar to duplex streams, but perform data conversion when writing and reading. Example: read or write data from/to a file.

Real-world Examples:

  1. Concurrent Request Handling: for example, an e-commerce website can handle simultaneous product searches, user registrations, and order placements without blocking the event loop, ensuring a smooth user experience.
  2. Non-Blocking Database Operations: for instance, a Node.js application can initiate multiple database requests simultaneously, such as fetching user profiles or updating inventory, without causing delays in other parts of the system.

Best Practices:

  1. Use Asynchronous APIs: this includes using asynchronous versions of functions or methods, such as fs.readFile instead of fs.readFileSync. By leveraging asynchronous nodejs APIs, you can avoid blocking the event loop and improve the responsiveness of your application.
  2. Error Handling: always implement error handling mechanisms, such as using try-catch blocks or attaching error callbacks, to gracefully handle exceptions.
  3. Avoid Callback Hell: use techniques such as modularization, named functions, or libraries like Promisify or Async.js to flatten the callback structure. This leads to cleaner and more maintainable code.

Tips for debugging and troubleshooting asynchronous code

Nodejs is without a shadow of a doubt one of the most used framework, therefore it presents several tips for debugging.

  1. Utilize proper error handling and logging: ensure you have proper error handling and logging mechanisms in place. Here are a few suggestions:

a. Use try-catch blocks: Surround the asynchronous code blocks with try-catch blocks to catch any synchronous errors that may occur.

b. If you’re using callbacks, follow the Node.js convention of using error-first callbacks. This means that the first parameter of the callback function is reserved for the error object. Check for errors in the callback and handle them appropriately.

c. Utilize promise rejections: If you’re working with Promises, use the .catch() method to catch any rejections and handle errors. Make sure you attach a .catch() to every Promise chain.

d. Use event listeners: If you’re dealing with event-driven code, attach event listeners to capture and handle errors that may be emitted.

e. Use a logging library like winston or debug to log errors, including stack traces, relevant context, and any other useful information. This will help you trace the flow of your asynchronous code and identify potential issues.

2. Leverage debugging tools and techniques: Node.js provides several debugging tools and techniques to help you troubleshoot asynchronous code effectively. Here are a few suggestions:

a. Debugging with console.log(): Use console.log() statements strategically throughout your code to log relevant values, states, and execution flow.

b. Node.js has built-in debugging support. You can use the --inspect flag when running your script and connect to it using a debugging client like Chrome DevTools or Visual Studio Code.

c. Node.js ecosystem offers various debugging tools and libraries like ndb, node-inspector, and debug. These tools provide enhanced debugging capabilities, such as advanced breakpoints, code stepping, and code profiling.

d. Analyze the stack trace provided. It can give you valuable information about the sequence of asynchronous calls and help pinpoint the source of the error.

e. Use linters and static analysis tools: Employ linters like ESLint and static analysis tools like SonarJS. They can detect potential issues, such as unused variables, incorrect variable types, or possible asynchronous code pitfalls.

By combining proper error handling and logging techniques with the right debugging tools and strategies, you can effectively debug and troubleshoot asynchronous code in Node.js.

Let’s call it a day!

You can explore a bit more about node.js if you want to! What is NodeJS used for? Here you’ll find useful info.

In future blog and instagram posts, we’ll explore specific strategies and techniques for using mobile web analytics to drive business growth and success.

So stay tuned, and don’t forget to check out our other posts for more insights on digital marketing and data analytics!