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?
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;
}