Here is a snippet of code showcasing a process involving multiple http requests and the use of promises:
The first step involves calling a service to initiate an http request. Upon receiving the response, a map is created based on the data, which will be used later in the process. Subsequently, a new http request is made within a data-table function, where operations are performed using the previously created map before displaying the data.
However, a challenge arises as there is a delay in obtaining the response from the initial $http call. The attempt to incorporate promises to ensure that the map is created before executing the second http call has not been successful. Any guidance on how to effectively use promises in this scenario would be greatly appreciated.
//Initiating a call to the service for the first http request
MasterServices.getAllCustomers().then(function(result) {
$scope.resultdata = result.data;
$scope.resultdata.forEach(element => {
//Creating a map holding id and name information
$scope.oumap.set(element.companyId, element.companyName)
});
});
//Configuring the Data-Table
vm.dtOptions = DTOptionsBuilder.fromFnPromise(function() {
var defer = $q.defer();
//Executing an http call to retrieve configuration data
MasterServices.getCompConfig().then(function(result) {
angular.forEach(result.data, function(val) {
if ($scope.oumap.has(val.compId)) {
val.companyName = $scope.oumap.get(val.compId);
} else {
val.companyName = " ";
}
});
defer.resolve(result.data);
});
return defer.promise;
}).withPaginationType('full_numbers').withOption('createdRow', createdRow);