Published
Edited
Nov 12, 2020
Importers
2 stars
Insert cell
Insert cell
Insert cell
function paginated_fetch(
url = is_required("url"), // Improvised required argument in JS
page = 1,
previousResponse = []
) {
return fetch(`${url}&page=${page}`) // Append the page number to the base URL
.then(response => response.json())
.then(newResponse => {
const response = [...previousResponse, ...newResponse]; // Combine the two arrays

if (newResponse.length !== 0) {
page++;

return paginated_fetch(url, page, response);
}

return response;
});
}
Insert cell
Insert cell
beers = paginated_fetch(`https://api.punkapi.com/v2/beers?`)
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more