2

I have a question about requests.I make first request then I suppose first onSuccess method will be runnning but program make second request immediately.How can i handle it? Context.getAlarmGroupById is called 2 times immediately. my code:

        function loadAll () {
        Context.getAlarmGroupChainById({
            id: $stateParams.id
        }, onSuccess, onError);


            function onSuccess(data, headers) {
            vm.temp=data; 
            var numberGroupChain=vm.temp.length;
            for(var i=0; i<numberGroupChain; i++){
                vm.location.push(vm.temp[i].location);
                vm.alarmChainList.push({name: vm.location[i],active:null,totalAlarmGroupNumber:null});             
                //loadData(vm.temp[i].id);
                asyncLoop(vm.temp[i].id);
              }
            }


            function onError(error) {
              AlertService.error(error.data.message);
            }

        var index = 0;

        function asyncLoop(chainId) {
            if(index >= vm.temp.length) {
                return;
            }
         Context.getAlarmGroupById({
             id:chainId
         }, onSuccess, onError);


             function onSuccess(data,headers){
                 index++;
                 asyncLoop();
             }

             function onError(data,headers){
                 index++;
                 asyncLoop();
             }
        }

    } 
Belhanda
  • 135
  • 1
  • 8

2 Answers2

1

Firstly: Car.getGroupId is an asynchronous function. So you have to ensure that the previous call is completed, before the next call.

Secondly: The recursive function is created for replacing the loop, after the success function and index increment, the recursive function is called to ensure your required requirement.

Third: Change the calling sequence of asyncLoop(vm.temp[i].id); after loop:

Finally:

Please use the following code:

function loadAll () {

    var index = 0;

    function asyncLoop(chainId) {
        if(index >= vm.temp.length) {
            return;
        }
     Context.getAlarmGroupById({
         id:vm.temp[index].id
     }, onSuccess, onError);


         function onSuccess(data,headers){
             index++;
             asyncLoop();
         }

         function onError(data,headers){
             index++;
             asyncLoop();
         }
    }

    function onSuccessParent(data, headers) {
        vm.temp=data; 
        var numberGroupChain=vm.temp.length;
        for(var i=0; i<numberGroupChain; i++){
            vm.location.push(vm.temp[i].location);
            vm.alarmChainList.push({name: vm.location[i],active:null,totalAlarmGroupNumber:null});             
            //loadData(vm.temp[i].id);

          }
          asyncLoop();
    }


    function onErrorParent(error) {
          AlertService.error(error.data.message);
    }

    Context.getAlarmGroupChainById({
        id: $stateParams.id
    }, onSuccessParent, onErrorParent);

}

I. Ahmed
  • 2,438
  • 1
  • 12
  • 29
0

Here is the pure JavaScript way to do this and it works perfectly fine. Use Promise.then(). Do like below:

var request1 = new Promise(function(resolve, reject) {
  resolve('Success!');
});

request1.then(function(value) {
  // make request2 here
});
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104