JAVASCRIPT
Fetch All Pages from a Paginated API (JavaScript Async/Await)
Learn to efficiently retrieve all data from a paginated API endpoint using JavaScript's async/await, iterating through pages until all results are fetched.
async function fetchAllPaginatedData(baseUrl, initialPage = 1, pageSize = 10) {
let allData = [];
let currentPage = initialPage;
let hasMorePages = true;
while (hasMorePages) {
try {
// Construct the URL with pagination parameters
const url = `${baseUrl}?page=${currentPage}&pageSize=${pageSize}`;
console.log(`Fetching page ${currentPage} from: ${url}`);
const response = await fetch(url);
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`HTTP error! Status: ${response.status}, Body: ${errorBody}`);
}
const pageData = await response.json();
// Assuming the API returns an array of items and a way to know if there are more pages
// Example: pageData = { items: [...], totalPages: X, currentPage: Y, hasNext: true/false }
if (!pageData || !Array.isArray(pageData.items)) {
console.warn("API response format unexpected, assuming no more data.");
hasMorePages = false;
break;
}
allData = allData.concat(pageData.items);
// Determine if there are more pages based on API response structure
// This logic depends on the specific API's pagination details
if (pageData.hasNext === false || currentPage >= pageData.totalPages) {
hasMorePages = false;
} else {
currentPage++;
}
} catch (error) {
console.error('Error fetching paginated data:', error);
hasMorePages = false; // Stop on error
}
}
return allData;
}
// Example usage:
// (async () => {
// const myData = await fetchAllPaginatedData('https://api.example.com/products', 1, 20);
// console.log('All fetched data:', myData);
// })();
How it works: This JavaScript snippet provides a robust way to fetch all available data from an API that uses pagination. It uses an `async` function with a `while` loop to sequentially request each page, accumulating the results until the API indicates there are no more pages. The logic for determining the next page and if there are more results (`hasNext`, `totalPages`) needs to be adapted to the specific pagination structure of the API being consumed.