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

231
Visualizações
How to get recursive function receiving promises to wait?

I am trying to get information from a database, where the information comes in multiple pages and all of it needs to be compiled to a single array.

However, even though I'm in a .then for handling the promise and I recursively call the function using the .then, it still doesn't wait and expects the "next page" array before I can grab it.

Below is the relevant code:

function getAllPersonEvents(jobID, pageNum) {
   getPersonEvents(jobID, pageNum).then(function(val){
      if (val.length < 100) {
         console.log(val);
         console.log("above returned");
         return val;
      } else {
         console.log("expecting return!");
         let nextPages = getAllPersonEvents(jobID, pageNum + 1);
         console.log(nextPages);
         let allEvents = val.concat(nextPages);
         console.log(allEvents);
         return allEvents;
      }
   });
}

function getPersonEvents(jobID, pageNum) {
   return fetch('WORKING FETCH URL' + pageNum + '&job_ids[]=' + jobID, options)
   .then(response => response.json())
   .then(response => {
      return response.person_events;
   })
   .catch(err => console.error(err));
}

How do I get to the "above returned!" code before the "expecting return!" part?

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

0

Your getAllPersonEvents never returns anything. Your then handler does, but not getAllPersonEvents. In order to do what you've described, you'll want to return a promise from it that will be fulfilled with the array.

Sticking with your explicit promise callbacks, you can do it like this (see comments):

function getAllPersonEvents(jobID, pageNum) {
    // An array to build up our results in
    const all = [];
    // Get the initial events
    return getPersonEvents(jobID, pageNum).then((page) => {
        all.push(...page);
        if (page.length < 100) {
            // We have all of them, fulfill the promise with the full array
            return all;
        } else {
            // There may be more, recurse...
            return getAllPersonEvents(jobID, pageNum + 1).then((page) => {
                // ...and then add the results from the recursion to our
                // own, and fulfill this promise (which fulfills the main
                // one) with `all`.
                all.push(...page);
                return all;
            });
        }
    });
}

It's much simpler to write using async/await, though:

function getAllPersonEvents(jobID, pageNum) {
    const all = [];
    let page;
    do {
        page = await getPersonEvents(jobID, pageNum++);
        all.push(...page);
    } while (page.length >= 100);
    return all;
}
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