Nice post, thanks for sharing. What do you think about integrating the clearing of the cache in the getData function? Performance wise it should be very similar and the result is even more compact (just 10 lines 😄)
const cache = {}; async function getData(url){ const currentTime = new Date(); if(cache[url] !== undefined) { if((currentTime - cache[url].time) < 10000) return cache[url].value; delete cache[url]; } await fetch(url) .then(response => response.json()) .then(json => cache[url] = {time: currentTime, value: json}); return cache[url].value; }
➕➕
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice post, thanks for sharing.
What do you think about integrating the clearing of the cache in the getData function?
Performance wise it should be very similar and the result is even more compact (just 10 lines 😄)
➕➕