Types of API functions in NodeJS

  • Asynchronous, Non-blocking functions
  • Synchronous, Blocking functions

Asynchronous, Non-blocking functions:

As the name suggests, these functions operate asynchronously. What it means is that when Node.js will make a request for data to the API, it will not get blocked till the data is received. Instead, it will continue to move to the next API after calling it, and a notification mechanism from a Node.js event will respond to the server for the previous API call. To put it in layman’s terms, these functions allow working further while the request is being handled. Example: Emails, online forums

Synchronous, Blocking functions:

Contrary to asynchronous functions, synchronous functions act as blocking functions. What it means is that these functions will make the calling system wait for a response. Thus, when a system uses synchronous APIs, it expects to get immediate data when requests are made. These types of APIs are used where availability and connectivity are high and low latency is expected. To put it in layman’s terms, the application will request and wait for a response until the value is returned. Example: Instant messaging, video meetings

Example: Suppose we have a JSON file named data.json which contains certain data as below:

{
"name": "John",
"age": 50,
"gender": "male"
}

Now, we will use asynchronous and synchronous functions to read this data. Node.js has an in-built module named fs which stands for File System which can be used to interact with files in a modeled way. To use it, we need to require it as follows:

const fs = require('fs');

We have used the following two functions in our application:

  • To read the file asynchronously, we have used readFile() method supported by the fs module.
  • To read the file synchronously, we have used readFileSync() method supported by the fs module.

Example: Implementation to show synchronous and asynchronous example.

Node
// index.js

// Requiring inbuilt module
const fs = require('fs');

// Asynchronous function
fs.readFile('data.json', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log("Below is the Data from Asynchronous function call")
  console.log(data);
});

// Synchronous function
var data = fs.readFileSync('data.json','utf8');
console.log("Below is the Data from Synchronous function call")
console.log(data);

Step to Run Application: Run the application using the following command from the root directory of the project

node index.js

Output: We will see the following output on your terminal screen:

Below is the Data from Synchronous function call
{
"name": "John",
"age": 50,
"gender": "male"
}
Below is the Data from Asynchronous function call
{
"name": "John",
"age": 50,
"gender": "male"
}

When the above code is executed, it will produce the same output as asynchronous, and in the same time frame. The reason behind this is that when we are reading one or two files, the difference between both methods won’t be significant. But when we are operating with a database and handling multiple requests, the difference will be quite clear as it will directly affect the performance.



Types of API functions in Node.js

Node.js, known for its asynchronous and event-driven architecture, is a popular choice for building scalable server-side applications. One of its primary uses is to create and manage APIs (Application Programming Interfaces). APIs allow different software systems to communicate and share data with each other. In Node.js, API functions can be categorized based on their nature, usage, and the type of data they handle. This article explores various types of API functions in Node.js, providing insights into their functionalities and use cases.

Table of Content

  • HTTP API Functions
  • WebSocket API Functions
  • File System API Functions
  • Database API Functions
  • Stream API Functions
  • Error Handling API Functions
  • Types of API functions in NodeJS

Similar Reads

HTTP API Functions

GET...

WebSocket API Functions

Connect...

File System API Functions

Read...

Database API Functions

Query...

Stream API Functions

Read Stream...

Error Handling API Functions

Catch...

Types of API functions in NodeJS

Asynchronous, Non-blocking functionsSynchronous, Blocking functions...