Skip to content

Sugestion withPagination #33

@h3rl

Description

@h3rl

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 }
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions