Class: Celluloid::Promise::Deferred

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid-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.

Instance Method Summary collapse

Constructor Details

#initialize(reactor) ⇒ Deferred

Returns a new instance of Deferred.



153
154
155
156
157
158
159
# File 'lib/celluloid-promise/q.rb', line 153

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

Instance Method Details

#promiseObject

Creates a promise object associated with this deferred



194
195
196
# File 'lib/celluloid-promise/q.rb', line 194

def promise
	DeferredPromise.new( self, @reactor )
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.

Parameters:

  • reason (Object) (defaults to: nil)

    constant, message, exception or an object representing the rejection reason.



187
188
189
# File 'lib/celluloid-promise/q.rb', line 187

def reject(reason = nil)
	resolve(ResolvedPromise.new(@reactor, reason, true))
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.

Parameters:

  • val (Object) (defaults to: nil)

    constant, message or an object representing the result.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/celluloid-promise/q.rb', line 166

def resolve(val = nil)
	@reactor.async.perform 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