Class: EventMachine::Q::Deferred

Inherits:
EventMachine::Q show all
Defined in:
lib/em-promise/q.rb

Overview

The purpose of the deferred object is to expose the associated Promise instance as well as APIs that can be used for signalling the successful or unsuccessful completion of a task.

Constant Summary

Constants inherited from EventMachine::Q

VERSION

Instance Method Summary collapse

Constructor Details

#initializeDeferred



226
227
228
229
230
231
# File 'lib/em-promise/q.rb', line 226

def initialize
  super()
  
  @pending = []
  @value = nil
end

Instance Method Details

#promiseObject

Creates a promise object associated with this deferred



266
267
268
# File 'lib/em-promise/q.rb', line 266

def promise
  DeferredPromise.new( self )
end

#reject(reason = nil) ⇒ Object

rejects the derived promise with the reason. This is equivalent to resolving it with a rejection constructed via Q.reject.



259
260
261
# File 'lib/em-promise/q.rb', line 259

def reject(reason = nil)
  resolve(Q.reject(reason))
end

#resolve(val = nil) ⇒ Object

resolves the derived promise with the value. If the value is a rejection constructed via Q.reject, the promise will be rejected instead.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/em-promise/q.rb', line 238

def resolve(val = nil)
  EM.schedule do
    if not @pending.nil?
      callbacks = @pending
      @pending = nil
      @value = ref(val)
      
      if callbacks.length > 0
        callbacks.each do |callback|
          @value.then(callback[0], callback[1])
        end
      end
    end
  end
end