Pagination

Many methods in the deezer-ts library return paginated results. The library provides a PaginatedList class to handle these results efficiently.

The PaginatedList class implements the async iterator protocol, allowing you to iterate through all items using a for await...of loop:

const artist = await client.getArtist(27);
const albums = await artist.getAlbums();

// Iterate through all albums
for await (const album of albums) {
console.log(album.title);
}

You can access information about the current page and total items:

const albums = await artist.getAlbums();

console.log(albums.total); // Total number of items
console.log(albums.limit); // Items per page
console.log(albums.hasMore); // Whether there are more items

If you need more control over pagination, you can manually navigate through pages:

const albums = await artist.getAlbums();

// Get the first page
const firstPage = await albums.items();

// Get the next page
if (albums.hasMore) {
await albums.next();
const secondPage = await albums.items();
}

You can use the slice method to get a specific range of items:

const albums = await artist.getAlbums();

// Get items 10-20
const slicedAlbums = await albums.slice(10, 20);
for await (const album of slicedAlbums) {
console.log(album.title);
}

The PaginatedList class fetches pages on demand, which means:

  • Memory usage is optimized as only one page is loaded at a time
  • Network requests are made only when needed
  • You can break out of iteration early without fetching unnecessary pages