I am trying to extract data from an API that returns only 1000 elements per call, and I want to do it recursively until I have all the data.
I don’t know in advance how many items there are, so after each call I have to check
If the call was synchronous, I would use something like this:
function fetch(all, start) {
const newData = getData(start, 1000);
all = all.concat(newData);
return (newData.length === 1000) ? fetch(all, all.length) : all;
}
However, the call getData()here is asynchronous. Using Promise.all()does not work, because I do not know in advance how many calls I need, so I can not prepare an array of calls.
I have a feeling that I can solve this with generators or with async/ await, but I don’t know how to do it. Can someone point me in the right direction?
In case that matters, I use Angular 4.