Recursive call to asynchronous API call

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.

+4
2

, . , getData , :

async function fetch(all, start) {
    const newData = await getData(start, 1000);
    all = all.concat(newData);
    return (newData.length === 1000) ? await fetch(all, all.length) : all;
}
+4

async/await :

let fetch = async () {
  try {
    let start = 0;
    let all = [];
    let newData;
    do {
      newData = await getData(start, 1000);
      start += newData.length;
      all = all.concat(newData);
    } while (newData.length > 0);

    return all;
  } catch (err) {
    console.log(err);
  }
}
+3

Source: https://habr.com/ru/post/1684212/


All Articles