Created
September 2, 2020 08:32
-
-
Save amatiasq/2458e2b549d2ff6576c25bebbb3f1369 to your computer and use it in GitHub Desktop.
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
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; | |
} |
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
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