JAVASCRIPT
Efficiently Handling API Pagination for Large Datasets
Master fetching paginated data from REST APIs. Learn to make sequential requests to retrieve all pages of a dataset using a loop and async/await for complete data retrieval.
async function fetchAllPaginatedData(baseUrl, pageSize = 10, initialPage = 1) {
let allItems = [];
let currentPage = initialPage;
let hasMore = true;
while (hasMore) {
try {
const url = `${baseUrl}?page=${currentPage}&pageSize=${pageSize}`;
const response = await fetch(url);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || `HTTP error! Status: ${response.status}`);
}
const { data, totalPages, currentPage: returnedPage } = await response.json(); // Adjust to your API's response structure
allItems = allItems.concat(data);
currentPage++;
// Determine if there are more pages based on API response (adjust logic as needed)
hasMore = returnedPage < totalPages; // Common pattern
// Or check if data array is empty: hasMore = data.length === pageSize;
// Or check for a 'next_page_url' property
} catch (error) {
console.error(`Error fetching page ${currentPage}:`, error);
hasMore = false; // Stop fetching on error
throw error;
}
}
return allItems;
}
// Example Usage (assuming API returns { data: [...], totalPages: N, currentPage: M })
// async function loadAllItems() {
// try {
// const allProducts = await fetchAllPaginatedData('https://api.example.com/products', 20);
// console.log('All products:', allProducts);
// } catch (err) {
// console.error('Failed to load all products:', err);
// }
// }
// loadAllItems();
How it works: This snippet provides a robust function to retrieve all data from an API that uses pagination. It iteratively fetches pages until all available data has been collected, using a `while` loop and `async/await`. The `hasMore` logic needs to be adapted to the specific API's response structure (e.g., checking `totalPages`, `next_page_url`, or the length of the returned `data` array).