Last active
July 13, 2020 06:48
-
-
Save omidnikrah/bde4991832769cb3517ae11137060d6f to your computer and use it in GitHub Desktop.
Simple Cashe API Requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CacheRequests { | |
constructor() { | |
this.caches = new Map(); | |
} | |
set(url, response) { | |
this.caches.set(url, response); | |
} | |
get(url) { | |
return this.caches.get(url); | |
} | |
has(url) { | |
return this.caches.has(url); | |
} | |
} | |
const cache = new CacheRequests(); | |
const Request = (url, options) => { | |
if (cache.has(url)) { | |
return cache.get(url); | |
} | |
fetch(url, options).then((response) => { | |
console.log(response); | |
cache.set(url, response); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment