AngularJS Avoid calling same REST service twice before response is received
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In AngularJS applications, duplicate HTTP calls are a common source of latency and confusing UI behavior. They usually happen when users click quickly or when multiple components request the same resource at nearly the same time. A good solution is to deduplicate in-flight requests and expose clear loading state to the view.
Why Duplicate Calls Happen
AngularJS executes asynchronous code quickly, and each controller or component can trigger its own request lifecycle. If two parts of the page call the same endpoint before the first response finishes, the server receives redundant traffic.
A simple example:
If the button is clicked twice, getProfile runs twice unless the service guards against reentry.
You can reduce accidental repeats at the UI level, but the durable fix belongs in the service layer because many callers may reuse the same method.
Coalesce In-Flight Requests with a Promise Cache
A practical pattern is to keep one in-flight promise per request key. If another call arrives while the first call is pending, return the same promise instead of issuing a new request.
This design gives three benefits:
- only one network request per key while pending,
- all callers receive the same resolved data,
- cleanup happens in
finallyso the next refresh can run.
If your endpoint returns large payloads and does not change often, add a second cache for resolved data and a time to live value. That avoids network calls even after the first request completes.
Guard the UI and Keep State Explicit
Service deduplication is important, but users still need visual feedback. Disable action controls while loading so interaction remains clear.
Template side:
The controller guard protects against fast repeated clicks, and the service guard protects against multi-caller duplication.
Add Request Cancellation for Rapid Navigation
In list and search pages, users may trigger many requests by typing quickly or switching views. You can cancel stale requests by passing a timeout promise to $http.
This does not deduplicate by key, but it prevents wasted work on requests you no longer need.
Common Pitfalls
A common mistake is placing the in-flight map in a controller, which breaks deduplication across pages. Keep it in a singleton service.
Another issue is forgetting to delete request keys when an error occurs. Always clean up in finally, not only in success handlers.
Some teams rely only on ng-disabled and think duplicates are solved. That helps for button clicks, but programmatic calls from route hooks or watchers can still issue repeated requests. Keep both UI guard and service guard.
Summary
- Duplicate REST calls usually come from rapid user actions or multiple callers.
- Use a service-level in-flight promise cache keyed by request identity.
- Return the same pending promise to all callers until completion.
- Reflect loading state in the UI and disable controls while pending.
- Use cancellation for search and navigation flows where stale requests are common.

