Method: Async::Promise#then

Defined in:
lib/async/promise.rb

#then(on_resolve = nil, on_reject = nil) ⇒ Promise

Returns a new promise, so that multiple [then] and [catch] calls can be chained.

Parameters:

  • on_resolve ((value) => next_value_or_promise, nil) (defaults to: nil)

    the function to call when the promise is resolved with a value.

  • on_reject ((reason) => next_value_or_promise, nil) (defaults to: nil)

    the function to call when the promise is rejected (either manually or due to a raised error).

Returns:

  • (Promise)

    returns a new promise, so that multiple [then] and [catch] calls can be chained.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/async/promise.rb', line 202

def then(on_resolve = nil, on_reject = nil)
  chainable_promise = self.class.new(on_resolve, on_reject)

  Async do |task|
    case @status
    when "pending"
      # add the new promise as a child to the currently pending promise. once this promise resolves, the new child will be notified.

      @children << chainable_promise
    when "fulfilled"
      # this promise has already been fulfilled, so the child must be notified of the resolved value immediately.

      chainable_promise.resolve(@value)
    when "rejected"
      # this promise has already been rejected, so the child must be notified of the rejection reason immediately.

      chainable_promise.reject(@reason)
    end
  end

  chainable_promise
end