API Calls Evolution(Callback vs Promise vs Observables)

Application without API calls is very less in the market. At least they will send the app analytics details to the backend to improve their application feature. Making API calls is simple but it will have a lot of complexities in the implementation. As part of javascript, making API calls is a very critical part in terms of performance. We shouldn't block the thread until we receive the data. The application needs to know when the data is returned. Then its needs to execute the data processing. Execute the data processing based on the API response will be simple or complex based on the business logic. Data processing parts have been improved a lot based on the developer's pain points. I have started my developments with callbacks, then promises, and now Observer. In each phase, they have addressed a lot of problems.

Callback

Calling the function once a response is received. The response will be success or failure. Based on that function will get executed. This is a simple use case. But in a real-time application will not be like that. Multiple API calls need to happen. We will make chains of calls one after another. It will increase the complexities and reduce the application response time. In some scenarios, we can make list API calls in parallel and we can complete the process once all it's completed or anyone from the list is completed. It's purely based on business logic. But Complete the process based on all or anyone from the list of APIs, is a complex process. Using promise we can implement it in simple ways.

Promise

It's an improved callback function. We can improve application performance in multiple API calls. It provides a built-in function like all and any to makes multiple API calls in parallel. It's increases readability and application performance. As part of the application evolving we need to add a new feature like cancel the API calls or continuously load a new set of data or load the promise on time(lazyload) instead of early(eager load). Using observer we can implement in simple ways.

Observables

This function gives a stream of values. It will returns data multiple times until it's complete. Using this we can fetch data continuously. It's will get executed once it's subscribed(lazyload) instead of early(eager load). Promise will push the response once it gets executed. Observables are pull models and promises are push models. By unsubscribing observers, we can cancel the API calls. It provides a lot of improvements over-promise.


Next Recommended Reading Find() vs. Filter() in JavaScript