//For example when we have a function responsible for retrieving
//and returning data we can use a strategy
//called memoization to retrieve data from the cache
//if it was already fetched before:
const axios = require('axios')
let fetchCount = 0
function memoize(fn) {
const cache = {}
return async function (key, ...args) {
if (cache[key]) {
return cache[key]
}
fetchCount++
return (cache[key] = await fn(...args))
}
}
const fetchDogs = memoize(function (page) {
if (typeof page !== 'number') page = 1
return axios
.get(
`https://dog.ceo/api/breeds/list/all
`,
)
.then((response) => {
return response.data.message
})
.catch((err) => {
throw err
})
})
async function start() {
try {
let dogs = await fetchDogs(`dogs_page_11`, 11)
return dogs
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error))
console.error(`Error: ${err.message}`, err)
}
}