I have an url of the pattern http://www.data.com/1, where the 1 at the end can run up until a not predefined number. It returns an array. I need to concatenate all the arrays I get into one.
My strategy is to recursively perform a get-request, until I get a 404 error, upon which I return the result.
var returnArray = []; function getData(count){ let p = axios.get(`http://www.data.com/${count}`).then(function(response){ returnArray = returnArray.concat(response.data); return getData(count +1); } ).catch(function() { Promise.resolve(returnArray); } )}; When I implement this function, however, my result is undefined.
getData(1).then((response) => console.log(response)) What is the best way to do this?
async/awaitand awhileloop which runs until it gets 404 response.getLeadData?getLeadDatashould return promise also?pfrom yourgetDatafunction, but because you expect to get the result fromcatchdid you try to return this also?:return Promise.resolve(returnArray);getLeadDatais equal togetData.