Created
May 15, 2014 11:04
-
-
Save benjamingr/5a9c269cdfda3b484f39 to your computer and use it in GitHub Desktop.
Promise cache once
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
// 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