Module: Cql::FutureFactories

Included in:
Future
Defined in:
lib/cql/future.rb

Instance Method Summary collapse

Instance Method Details

#all(*futures) ⇒ Cql::Future<Array>

Combines multiple futures into a new future which resolves when all constituent futures complete, or fails when one or more of them fails.

The value of the combined future is an array of the values of the constituent futures.

Parameters:

  • futures (Array<Cql::Future>)

    the futures to combine

Returns:

  • (Cql::Future<Array>)

    an array of the values of the constituent futures



90
91
92
93
94
95
96
# File 'lib/cql/future.rb', line 90

def all(*futures)
  if futures.any?
    CombinedFuture.new(futures)
  else
    resolved([])
  end
end

#failed(error) ⇒ Cql::Future

Creates a new pre-failed future.

Parameters:

  • error (Error)

    the error of the created future

Returns:



124
125
126
# File 'lib/cql/future.rb', line 124

def failed(error)
  FailedFuture.new(error)
end

#first(*futures) ⇒ Cql::Future

Returns a future which will be resolved with the value of the first (resolved) of the specified futures. If all of the futures fail, the returned future will also fail (with the error of the last failed future).

Parameters:

  • futures (Array<Cql::Future>)

    the futures to monitor

Returns:

  • (Cql::Future)

    a future which represents the first completing future



104
105
106
107
108
109
110
# File 'lib/cql/future.rb', line 104

def first(*futures)
  if futures.any?
    FirstFuture.new(futures)
  else
    resolved
  end
end

#resolved(value = nil) ⇒ Cql::Future

Creates a new pre-resolved future.

Parameters:

  • value (Object, nil) (defaults to: nil)

    the value of the created future

Returns:



116
117
118
# File 'lib/cql/future.rb', line 116

def resolved(value=nil)
  ResolvedFuture.new(value)
end