0

Hi we have an angular js controller that call a service function:

var decodedValue = StaticTableService.DecodificaAllergeni(listObj);

this service function get an array of integer. We want to decode this integer in string from StaticTableService. There decodedValue will be a string array, where each string id the decoded string from int. If int number is 0 not be decoded.

This functiong

 DecodificaAllergeni: function(allergeni)
    {
    var elenco = Array();

    var loopPromises = [];                      //Promise for loop
    for(i=0;i<allergeni.length;i++)
    {
        var idAllergene = allergeni[i];
        if(idAllergene!=0)
        {
            console.log("StaticTable IdAllergene: "+idAllergene);
            loopPromises.push(AllergeniService.get(idAllergene));
            console.log(idAllergene);
        }
    }

    //Wait all 
    $q.all(loopPromises).then(function () {
        console.log('forEach loop completed.');
    });
    },

I declare promise array and wait $q.all(...) for waiting all call are ending.

Every call is made to AllergeniService.get(idAllergene) where AllergeniService is another service with same element and i match single id with id element. If match return a single element.

get: function(idAllergene)
     {
       var deferred = $q.defer();                 
       for (i = 0; i < allergeni.length; i++) {

          //if(idAllergene == allergeni[i].id) 
          //   ...
       }
       console.log("Id allergene che ha chiamato: "+idAllergene);
       deferred.resolve(1);
       return deferred.promise;                  
     }

In this case a return always deferred resolve with 1 as parameter. The problem is that this call is made only one time with first element.

How can call a function n times with different parameter and wait until all call are terminated?

Thanks

Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53

0 Answers0