Variable not defined

yes… corrected… checking again.

will post update here soon

Its still asynchronous

Controller code

CustomerService.retrieveSearchCustomer($scope.caasCustomerSearch).then(function(data){
								console.log("result$$$$$"); //Line 1
								$scope.customerSearchOutputLst= data;
								console.log($scope.customerSearchOutputLst);
								console.log("############");
							})
							.catch(function(response){
								console.log(response.status);
							}); 
							
							
console.log("Hello");  // Line 2

Service code

app.service('CustomerService', ['$http','$q', function ($http,$q) {

this.retrieveSearchCustomer = function (newCustomerSearch) {
			var deferred = $q.defer();
			$http.post(customerUrl + '/searchcustomer', newCustomerSearch)
			.then(function(response){
			   deferred.resolve(response.data);
			})
			.catch(function(response){
			  deferred.reject(response);
			});
			return deferred.promise;
		}
}]);

Output

//Line 2 prints first
//Line 1 prints second

As expected.

what to change then ?

I expected to see print in a synchronous way
//Line 1 prints first
//Line 2 prints second

CustomerService.retrieveSearchCustomer($scope.caasCustomerSearch)
    .then(function(data){
        console.log("result$$$$$") //Line 1
        $scope.customerSearchOutputLst= data
        console.log($scope.customerSearchOutputLst)
        console.log("############")
    })
    .then(function () {
        console.log("Hello") //Line 2
    })
    .catch(function(response){
        console.log(response.status)
    })

It did not work.

code

CustomerService.retrieveSearchCustomer($scope.caasCustomerSearch).then(function(data){
	console.log("result$$$$$");
	$scope.customerSearchOutputLst= data;
	console.log($scope.customerSearchOutputLst);
	console.log("############");
	}).then(function () {
			console.log("Hello") //Line 2
	})
	.catch(function(response){
		console.log(response.status);
});

output:

Hello
result$$$$$

Its still not sequential and synchronous.

That makes no sense. If the service returns a promise, the sequence must be as defined.

okay …I’;m clearing browser cache … going for a second run … I’ll post second output soon.

phew … second run output and code

Controller code:

CustomerService.retrieveSearchCustomer($scope.caasCustomerSearch).then(function(data){
	console.log("result*****");
	$scope.customerSearchOutputLst= data;
	console.log($scope.customerSearchOutputLst);
	console.log("############");
	}).then(function () {
			console.log("Hell") //Line 2
	})
	.catch(function(response){
		console.log(response.status);
	});

Output

==========
CustomerController.js:1515 result*****
CustomerController.js:1517 [{…}]
CustomerController.js:1518 ############
CustomerController.js:1520 Hell

this looks fine to me now.

Now I am planning to call another http request at //Line 2 and load another list there .

shall I proceed with similar process at //Line 2 with $q ?

my whole objective is to call two http url separately and load data in list separately and sequentially and compare two list and move on next part of controller based on some condition coming out of comparison.

see post #18. you can load the lists separately, but loading them sequentially would make the processing code a PITA. therefore load them in parallel via Promise.all() (or the Angular equivalent thereof) so that you receive the data that you want to compare in the same callback, where you can do the comparison.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.