Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
promise .then() to run sequentially inside a for loop

I'm trying to fill my $scope.results = [] sequentially by multiple sequential API calls. sequential is important as i'm passing in a start and a limit number (something like pagination). There is a reason that I cannot do this in 1 call and need to break into multiple calls since the API gateway timeouts after 30s.

My problem is in the for loop it doesnt work sequentially. Inside the array all network requests seem to get the same start values and are not sent sequentially. I also cannot use async/await. Is there a way to run the API calls sequentially and get add the result to the results array. My final goal is to fill the array like it happened in a single API call but with multiple calls

         $scope.detailedReportOfAll = function () {
            $scope.start = 0;
            $scope.limit = 15;
            $scope.results = [];
            var requestCount = 1;
            APIService.getDetailedReportData($scope.start,$scope.limit).then(
              function ({result,nbPages}) {
                if (result.length){
                  $scope.results = $scope.results.concat(result);
                  requestCount ++;
                  $scope.start += $scope.limit;
                }
                if (requestCount < nbPages){ //I'm running the loop for (nbPages - 1) times

                  for (i=2; i<nbPages; i++){
                    APIService.getDetailedReportData($scope.start,$scope.limit).then(
                      function ({result,nbPages}) {
                        if (result.length){
                          $scope.results = $scope.results.concat(result);
                          requestCount ++;
                          console.log($scope.results, requestCount)
                          $scope.start += $scope.limit;
                        }
                      })
                  }
                }
              }
            );
          };

this is my http calling function. it returns a promise

    var getDetailedReportData= function (start, limit) {
      $rootScope.isLoading = true;
      var deferred = $q.defer();
      $http
        .get(rootURL('/getAllReports/'+start+'/'+limit))
        .success(function (response) {
          deferred.resolve(response);
        })
        .catch(function (err) {
          deferred.reject(err);
        })
        .finally(function () {
          $rootScope.isLoading = false;
        });
      return deferred.promise;
    };

Help is much appreciated

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to run these in series then you need to trigger async function n when you know that async function n-1 is finished; i.e. in the then callback. Calling a function recursively is likely to be the easiest way to do this if you've rejected async and await.

function somethingAsync(value) {
    return new Promise(res => setTimeout(res, 500, value * value));
}

function processInputInSeries(input) {
    function processNext(index, input, results) {
        if (index >= input.length) return results;
        return somethingAsync(input[index]).then(function (result) {
            return processNext(index + 1, input, [...results, result]);
        });
    }

    return processNext(0, input, []);
}

const resultPromise = processInputInSeries([1,2,3]);
resultPromise.then(function(results) { console.log(results) });

For comparison, async/await is much clearer:

function somethingAsync(value) {
    return new Promise(res => setTimeout(res, 500, value * value));
}

async function processInputInSeries(input) {
    let results = [];
    for (let i = 0; i < input.length; i++) {
        const result = await somethingAsync(input[i]);
        results = [...results, result];
    }
    return results;
}

const resultPromise = processInputInSeries([1,2,3]);
resultPromise.then(function(results) { console.log(results) });

I would recommend writing it that way even if you end up using Babel to transpile your JS to be compatible with outdated systems.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda