Skip to content

Offset (multiple API Calls per parameter) causes fetching problems #769

Answered by moonwave99
gregorsart asked this question in Web
Discussion options

You must be logged in to vote

Exactly @NikRoe !

If you want to refactor it a bit and make it a bit more functional, this may be interesting:

async function getResponse({ barking, offset }) {
  const params = new URLSearchParams({ barking, offset });
  const response = await fetch(
    `https://api.api-ninjas.com/v1/dogs?${params}`,
    {
      headers: {
        'X-Api-Key': process.env.DOG_API_KEY,
      },
    }
  );
  return await response.json();
}

async function getAllResponses(barking) {
  const totalRecords = 200;
  const recordsPerPage = 20;
  // generates an array like [0, 20, 40, ..., 180]
  const offsets = Array.from(
    { length: Math.floor(totalRecords / recordsPerPage) },
    (_, index) => index * reco…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
4 replies
@gregorsart
Comment options

@NikRoe
Comment options

@NikRoe
Comment options

@moonwave99
Comment options

Answer selected by NikRoe
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Web
Labels
None yet
3 participants