Last active
September 29, 2016 19:57
-
-
Save alex-r-bigelow/0dc0fa0b7d6f0ada9154f3486675028a to your computer and use it in GitHub Desktop.
Demo of my strange caching strategy
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
/*globals jQuery*/ | |
function createPromise (url) { | |
return new Promise((resolve, reject) => { | |
jQuery.ajax({ | |
url: url, | |
success: resolve, | |
error: reject | |
}); | |
}); | |
} | |
class Cache { | |
constructor () { | |
this._cachedPromises = {}; | |
} | |
get lukeSkywalker () { | |
if (!this._cachedPromises.lukeSkywalker) { | |
this._cachedPromises.lukeSkywalker = createPromise('http://swapi.co/api/people/1'); | |
} | |
return this._cachedPromises.lukeSkywalker; | |
} | |
clearLukeSkywalker () { | |
delete this._cachedPromises.lukeSkywalker; | |
} | |
get tatooine () { | |
if (!this._cachedPromises.tatooine) { | |
this._cachedPromises.tatooine = this.lukeSkywalker.then(luke => { | |
return createPromise(luke.homeworld); | |
}); | |
} | |
return this._cachedPromises.tatooine; | |
} | |
} | |
let test = new Cache(); | |
test.tatooine.then(response => console.log(response)); | |
test.lukeSkywalker.then(response => { console.log(response); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment