Plan
Premium
Country
United States
Device
DELL G5 5500
Operating System
Microsoft Windows 11 Home
My Question or Issue
I'm using the TypeScript SDK (@spotify/**bleep**) to write a simple application. I wrote a function to get all of the current user's playlists:
import { Page, SimplifiedPlaylist, SpotifyApi } from "@spotify/web-api-ts-sdk";
export async function listUserPlaylists(
spotify: SpotifyApi,
): Promise<SimplifiedPlaylist[]> {
// TODO: For some reason, this yields a duplicate, but ONLY between the first
// and second pages. The last playlist of the first page and first playlist of
// the second page is always the same, no matter what `limit` I try with.
async function* iterUserPlaylists(): AsyncGenerator<SimplifiedPlaylist> {
const limit = 5; // TEMP: small number to test pagination.
let offset = 0;
let page: Page<SimplifiedPlaylist>;
do {
console.log(`OFFSET IS NOW ${offset}`);
page = await spotify.currentUser.playlists.playlists(limit, offset);
for (const playlist of page.items) {
console.log(`Yielding ${playlist.id}`);
yield playlist;
}
offset += page.items.length;
} while (page.next !== null);
}
const playlists: SimplifiedPlaylist[] = [];
for await (const playlist of iterUserPlaylists()) {
playlists.push(playlist);
}
return playlists;
}
In other words, this function basically collects playlists from the pages returned by the API into a single array. However, there's an interesting bug. The last playlist of the first page and the first playlist of the second page are always the same. That is, the print statements above give something like this (IDs abbreviated):
OFFSET IS NOW 0
Yielding 5FpuSaX...
Yielding 2lqJXz2...
Yielding 37i9dQZ...
Yielding 3JpajNp...
Yielding 065falt... // <-- THESE ARE THE SAME
OFFSET IS NOW 5
Yielding 065falt... // <-- THESE ARE THE SAME
Yielding 1RPJbcJ...
Yielding 37i9dQZ...
Yielding 7JcOISX...
Yielding 2X68da6...
OFFSET IS NOW 10
Yielding 4GknCaI...
Yielding 5hYYpZ2...
Yielding 37i9dQZ...
Yielding 6sUTQzd...
Yielding 4AApsy0...
OFFSET IS NOW 15
Yielding 37i9dQZ...
Yielding 6Ce4BCm...
Yielding 27e31bd...
Yielding 0vPgi1o...
Yielding 0CfW0t7...
// More output not shown.
This happens no matter what `limit` I try (e.g. 5 per page, 7 per page, 10 per page, etc.); the first and second pages always have that one duplicate. I tried rewriting it without the async generator, by using recursion etc. but they're all functionally equivalent. Is there a problem in my implementation logic? Or is this a bug in the endpoint?