lib/whenner/deferred.rb in whenner-0.1.1 vs lib/whenner/deferred.rb in whenner-0.2.0

- old
+ new

@@ -103,9 +103,29 @@ # @return [Promise] def to_promise promise end + + # Register a callback to be run when the deferred is fulfilled. + # + # @yieldparam [DeferredProxy] proxy + # @return [Promise] a new promise representing the return value + # of the deferred, or -- when that return value is a promise itself + # -- a promise mimicking that promise. + def then + Whenner.defer do |d| + proxy = DeferredProxy.new(self) + yield proxy + pdone = done(&proxy.done) + pfail = fail(&proxy.fail) + pdone.done { |v| d.fulfill(v) } + pdone.fail { |v| d.reject(v) } + pfail.done { |v| d.reject(v) } + pfail.fail { |v| d.reject(v) } + end + end + # Register a callback to be run when the deferred is fulfilled. # # @yieldparam [Object] value # @return [Promise] a new promise representing the return value # of the callback, or -- when that return value is a promise itself