-1

I have two angular.forEach

The second one musn't start untill the first one is finished :

$scope.rendezVous.j_plaignant = [];

angular.forEach(data.data,function(objet){
    if(objet.idpartie==1){
        $scope.rendezVous.j_plaignant.push(objet.idjuriste);
    }
})

$scope.rendezVous.juristes_plaignant = [];

angular.forEach(data.data,function(objet){
    if(objet.idpartie==1){
        $scope.rendezVous.juristes_plaignant.push(objet);
    }
})

I've tried this with no luck :

var defer = $q.defer();
$scope.rendezVous.j_plaignant = [];

angular.forEach(data.data,function(objet){
    if(objet.idpartie==1){
        $scope.rendezVous.j_plaignant.push(objet.idjuriste);
    }
})

$q.all($scope.rendezVous.j_plaignant).then(lastTask);

function lastTask(){
    $scope.rendezVous.juristes_plaignant = [];

    angular.forEach(data.data,function(objet){
        if(objet.idpartie==1){
            console.log(objet);
            $scope.rendezVous.juristes_plaignant.push(objet);
        }
    })
}

It would be nice if you have an idea, $q is difficult to understand.

I don't understand why i isn't possible to add a .then after a forEach., it is so annoying, i can't understand $q.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
helloguyz
  • 1
  • 1
  • https://stackoverflow.com/questions/43495864/can-anyone-explain-the-use-of-q-service-in-angularjs – logan Aug 18 '17 at 23:10
  • There is something missing from your code as the snippets shown are all synchronous. Are the two `forEach` blocks inside different `.then` blocks? Please clarify your question. – georgeawg Aug 18 '17 at 23:23
  • 1
    Not even clear why you need two forEach.....or what specific problem you are having – charlietfl Aug 18 '17 at 23:45
  • Why using `$q` is a requirement? Your first could should already do what you want, not starting the second `forEach` until the first one has finished. – Alisson Reinaldo Silva Aug 18 '17 at 23:48

1 Answers1

1

I don't think you need a defer.

When the second forEach occurs, the first is done. This is synchronous.

Ludovic Guillaume
  • 3,237
  • 1
  • 24
  • 41