Skip to content

Instantly share code, notes, and snippets.

@benjamingr
Created May 15, 2014 11:04
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 benjamingr/5a9c269cdfda3b484f39 to your computer and use it in GitHub Desktop.
Save benjamingr/5a9c269cdfda3b484f39 to your computer and use it in GitHub Desktop.
Promise cache once
// returns a version of a given promise returning function fn that always uses the same cached value
function once(fn){
var called = false, cache = null;
return function cachedPromise(){
if(called) return cache;
cache = fn.apply(this,arguments);
called = true;
return cache;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment