I just want to fetch data from multiple urls in a circle loop until the return data is less then the limited number of objects, every url return the array of data object, I would combine all of result in to an postResponse
array.
let urlsample = "https://example.com/page";
let postResponse = await page.evaluate(async (urlsample) => {
let promises = [], pagenumb = 1;
var fetchNow = function () {
fetch(urlsample + pagenumb + "&page_size=100").then(function (response) {
response.json().then(rp => {
promises.push(rp.data.list);
if (rp.data.list.length < 100) {
return promises;
}
else {
pagenumb++;
fetchNow();
}
})
}).catch(error => console.log(error));
}
fetchNow();
return Promise.all(promises);
}, urlsample);
console.log('Final response');
console.log(postResponse);//the console print []
//---------
postResponse.then(x => {console.log(x);});//the console print error then is not the function
My function is as above.I had tried many time to figure out the solution but seem stuck in how to use Promise.all in right way. Anyone can help me out?