Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sugestion withPagination #33

Open
h3rl opened this issue Nov 26, 2021 · 0 comments
Open

Sugestion withPagination #33

h3rl opened this issue Nov 26, 2021 · 0 comments

Comments

@h3rl
Copy link

h3rl commented Nov 26, 2021

i wont bother implementing this into the api, but think its a very usefull function, so thats why i wanna share it.

basicly an easy universal "wrapper" for pagination requests.

here you go :P

async function withPagination(func, args) {
  try {
    var result = await func(args);

    while (result.pagination.cursor) {
      const cursor = result.pagination.cursor;

      //console.log(`paginating ${cursor}`);
      const newargs = { ...args, after: cursor };

      // new response with cursor
      const resp = await func(newargs);

      // replace cursor
      result.pagination.cursor = resp.pagination.cursor;

      // append data
      for (let dat of resp.data) {
        result.data.push(dat);
      }
    }
    return result;
  } catch (err) {
    console.error(err);
  }
}

example usage:

async function getClips() {
  const users = await twitch.getUsers("someuser");
  const user = users.data[0];
  const broadcaster_id = user.id;

  const clips = await withPagination(
    async (args) => {
      return await twitch.getClips(args);
    },
    { broadcaster_id }
  );
}
@h3rl h3rl changed the title Sugestion Sugestion withPagination Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant