Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!