To retrieve exercise data from an API, you can implement a solution using a while
loop:
async function getExercises () {
let url = 'https://wger.de/api/v2/exercise/?format=json'
while (url) {
const res = await fetch(url)
const data = await res.json()
for (const item of data.results) {
console.log(item.name)
}
url = data.next
}
}
// It's important to handle errors as well, to prevent unhandled rejections.
// Ensure this code is executed within an async function to catch any errors that may occur.
getExercises().catch(e => console.error('Failed to get exercises', e))
In addition, I found out that specifying a limit
parameter works efficiently. By setting a higher limit like
https://wger.de/api/v2/exercise/?format=json&limit=1000
, it reduces the number of requests required. With only 685 results currently available, setting a limit of 1000 would retrieve all results in one request. Nonetheless, implementing the fetching logic is essential for future-proofing in case the dataset grows beyond the set limit.