Skip to content Skip to sidebar Skip to footer

Chaining Promise And Defered With A For Loop In Angular Application

I try to use promise and defered in an angular application. I use two functions $scope.checkQuestions() and $scope.saveExerciseApi() but the problem is that the second one is launc

Solution 1:

You resolve method is called as soon first question is saved. The resolve method should be called after adding all new question i believe

$scope.checkQuestions = function(){
deferred = $q.defer()
var questionsToSave=0;
for (i in $scope.formQuestion){
    if ($scope.formQuestion[i].pk == null){     //Save
        int questionsToSave=questionsToSave + 1;
        $scope.Questions.add($scope.formQuestion[i]).then(function(newquestion){
            $scope.formQuestion[i] = newquestion;
            if(questionsToSave<=1)
               deferred.resolve(newquestion);
            else
               questionsToSave=questionsToSave-1;
            });
        }    
    }
return deferred.promise;
}

Post a Comment for "Chaining Promise And Defered With A For Loop In Angular Application"