Skip to content

Instantly share code, notes, and snippets.

@azproduction
Last active December 14, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azproduction/5039683 to your computer and use it in GitHub Desktop.
Save azproduction/5039683 to your computer and use it in GitHub Desktop.
Promise-based cache
/**
* @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