Posts

Showing posts with the label axios

Calling APIs in sequence using async-await

Image
 There are certain cases when we want to call some APIs in a sequence (order) to get some data. For example, let's say we want to fetch data of 1000 students of an university for some analysis. The API given to us fetches only 100 records at a time but supports pagination. Therefore, we need to call the API 10 times, one after the other to get these records. This can be achieved by Using plain old callbacks. Using a combination of promises and callbacks. Using modern JavaScript feature - async-await (both async and await are keywords). In this article we will discuss method 3 to solve this problem. We will see the implementation in action using an open API. We will be creating a console app using NodeJS (version 14.7 as of writing) and will be using axios  for making http calls. Setting up the project Create a directory where you want to keep your JavaScript code files. I have named my directory "api-sequence-async-await". Open your terminal or command prompt and run co