Created
January 26, 2014 02:11
-
-
Save anonymous/8627124 to your computer and use it in GitHub Desktop.
Thunk: a Bluebird Promise whose resolver is only started when its value is needed
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
class Thunk extends Promise | |
evaluated: false | |
constructor: (resolver) -> | |
deferred = Promise.defer() | |
@eval = -> | |
new Promise(resolver).then( | |
deferred.resolve | |
deferred.reject | |
) | |
super deferred.promise | |
_addCallbacks: -> | |
if not @evaluated | |
@evaluated = true | |
@eval() | |
super arguments... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment