Open In App

Types of API functions in Node.js

Last Updated : 04 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

API stands for Application Programming Interface. It consists of various communication protocols and subroutines that can be used by programs for inter-communication. There are various types of APIs available such as WEB APIs, LOCAL APIs, PROGRAM APIs, etcetera. They are one of the most efficient and reliable ways to deliver output.

Types of API functions in Node.js:

  1. Asynchronous, Non-blocking functions
  2. 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:

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 index.js file using the following command.

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads