How To Make Parallel API calls in React Applications
A Step by step guide with an example project
In web applications, all the data you show on the page should reside somewhere, for example, cache, database, storage account, etc. You need to fetch the data from the different sources and do some processing and then render the data on the UI. All the data can be accessed through APIs nowadays and most of the time the format would be in the JSON format.
Sometimes, making one API call is not enough you might need to get the data from different sources and these sources might be different APIs. We can make all the calls in parallel to decrease the latency of the application.
In this post, we will see how to make parallel API calls in React applications using Fetch and Axios. You can do API calls with either of these.
- Prerequisites
- Example Project
- API Endpoints
- UI Implementation and Integration
- Call The APIs with Fetch and Promise.all
- Call The APIs with Axios and Promise.all
- Demo
- Summary
- Conclusion
Prerequisites
There are some prerequisites for this project. We need to have a NodeJS installed on your machine to install React-based dependencies. We need an editor to write the code. We are using a VSCode as an editor. There are many other editors as well to choose from such as web storm, atom, etc. Since we are writing the app in typescript you need to understand that as well.
Once you are on the NodeJS website you can install the LTS one. Click on it and follow the further instructions.
You can check whether NodeJS is installed or not with the following command. It gives you the version of the node is properly installed on your machine.
node -v
Let’s install the React CLI globally with the following commands. Make sure you install it globally so that you can use it from anywhere on your machine. The React CLI makes it easy to create an application that already works, right out of the box. You can generate components, etc.
// install globally
npm install -g create-react-app// check the version
npx create-react-app --version
Please go through these links if you are new to web development and make sure you understand how UI and API work together.
Example Project
Here is the example project in which we are making three API calls and combining them and loading the table. Those three calls take different times to complete.
// clone the project
git clone https://github.com/bbachi/react-api-parallel-calls.git
// ui
cd ui
npm install
npm start
// api
cd api
npm install
npm start
Here is the demo of the project.
We are making three API calls as shown below and we are waiting until all the calls are completed and then render the table in the UI.
API Endpoints
I have implemented three endpoints in NodeJS with the Express framework. You can refer to the below example project If you are not sure how to implement one.
How to write production-ready Node.js Rest API — Javascript version
How to write production-ready Node.js Rest API — Typescript version
Here is the simple server file which listens on port 3080 and exposes three endpoints. I am using setTimeOut function for the purpose of different latencies.