Last active
December 14, 2015 05:59
-
-
Save azproduction/5039683 to your computer and use it in GitHub Desktop.
Promise-based 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
/** | |
* @param {String} url | |
* | |
* @return {Promise} | |
*/ | |
function httpGet(url) {/*...*/} | |
var cache = {}; | |
module.exports = function (url) { | |
// if cacne -> return | |
// else request and cache on resolve | |
return cache[url] || httpGet(url).then(function (body) { | |
// cache and forward result | |
return cache[url] = promise, body; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment