I'm trying to fill an array with the result of multiple API calls using recursion. I'm pretty over my head here with ES6 and the recursion stuff and could need some help.
Here is my current code, which only returns "promise" :
getAllEmployees: function() {
let allEmployees = []; // this array will contain all employees
let pageNumber = 1; // start with page 1
const getThoseEmployees = function(pageNumber) {
return axios.get(rest_url + 'users/?per_page=50&page=' + pageNumber, {
headers: {
'X-WP-Nonce': WPsettings.nonce
},
}).then(response => {
// add the employees of this response to the array
allEmployees = allEmployees.concat(response.data);
pageNumber++;
if (response.headers['x-wp-total'] > allEmployees.length) {
// do me again...
return getThoseEmployees(pageNumber);
} else {
// this was the last page, return the collected contacts
return allEmployees;
}
});
}
return getThoseEmployees();
},
// after "created" in vue
this.allEmployees = this.getAllEmployees(); // returns "promise"