Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Created September 2, 2020 08:32
Show Gist options
  • Save amatiasq/2458e2b549d2ff6576c25bebbb3f1369 to your computer and use it in GitHub Desktop.
Save amatiasq/2458e2b549d2ff6576c25bebbb3f1369 to your computer and use it in GitHub Desktop.
let cache = null;
export function myCachedRequest() {
if (cache) {
return Promise.resolve(cache);
}
return fetch('potato.com/foo').then(x => {
cache = x;
return x
})
}
export function getCachedValue() {
return cache;
}
import { myCachedRequest, getCachedValue } from './cached-request.js';
// if cached it will be resolved immediately
// otherwise it will make a request
myCachedRequest().then(value => {
// ...
})
// syncronous but if the value is not cached yet returns null
const value = getCachedValue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment