Let's consider a scenario where we have a basic front end application (perhaps using Angular) and a back end app. When the front end app performs a get
request, in most cases, the Angular repository will initiate an $http.get
request which will return a promise (for Angular 1) or an observable that can be converted to a promise (for Angular 2 or 4). The repository then returns this promise and the Angular service would typically look something like this:
repository.makeTheGetCall().then(function (response) {
// process response
});
While this method is usually sufficient, it may not be ideal in certain situations.
1) What if the entire logic of the service relies on this single call? In such cases, we end up nesting the entirety of the service within a .then clause.
2) Another scenario could involve making subsequent requests based on the response of the initial Get request, followed by further requests depending on those responses. This leads to multiple chained then clauses.
Both of these situations are not uncommon and often result in what some may perceive as 'unattractive' code. Are there any alternative practices that can be employed to allow for asynchronous calls without the need to return promises from the repository layer to the service layer?
Your insights would be greatly appreciated. Thank you :)