Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda