I have this fiddle example, in which I'm making three consecutive calls to a service function (emulating $http Request interceptor function) returning a promise, the code is below. I want the second and next calls to wait until the previous finishes, because the second and next ones depend on the previous response. currently Im getting
Message 1: The value returned is 6000,
Message 2: The value returned is 600
Message 3: The value returned is 30
but I want to get
Message 1: The value returned is 10
Message 2: The value returned is 200
Message 3: The value returned is 6000
var myApp = angular.module('myApp', []);
myApp.factory('interceptor',['$q','$timeout',function($q,$timeout){
var _fact ={};
var asynTimeout;
var _intvalue = 1;
var _asyncTask = function(time, value){
var deferred = $q.defer();
asynTimeout = $timeout(function(){
_intvalue = _intvalue*value
deferred.resolve(_intvalue);},time)
return deferred.promise;
};
_fact.asyncTask = _asyncTask;
return _fact;
}]);
myApp.controller('AppCtrl',['$scope', 'interceptor',function ($scope,interceptor) {
interceptor.asyncTask(1500, 10).then(function(returnedval){
$scope.message1 = "The value returned is " + returnedval;});
interceptor.asyncTask(1000, 20).then(function(returnedval){
$scope.message2 = "The value returned is " + returnedval;});
interceptor.asyncTask(800, 30).then(function(returnedval){
$scope.message3 = "The value returned is " + returnedval;});
}])
the template would be:
<div ng-controller="AppCtrl">
<div>Message 1: {{message1}}</div>
<div>Message 2: {{message2}}</div>
<div>Message 3: {{message3}}</div>
</div>
Notice, the solution should be implemented in the factory function -nested calls at controller is not a solution for this scenario-, what I want is something as follow at interceptor side
myApp.factory('interceptor',['$q','$timeout',function($q,$timeout){
...
var _asyncTask = function(time, value){
var deferred = $q.defer();
***IF ITS RUNNING A PREVIOUS CALL WAIT FINISHES AND THEN(function(){***
asynTimeout = $timeout(function(){
_intvalue = _intvalue*value
deferred.resolve(_intvalue);},time)
***}**
return deferred.promise;
};
...
return _fact;
}]);