class Future(T)

Overview

A Future represents a value which may or may not currently be available, but will be available at some point, or an exception if that value could not be made available.

Defined in:

concur/future.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(&block : -> T) #

[View source]

Instance Method Detail

def await(t : Time::Span) : T | Exception #

Same as #await, but returns a Timeout exception if the given t expires.


[View source]
def await : T | Exception #

Awaits the completion of the future and returns either a value or an exception.


[View source]
def await!(t : Time::Span) : T #

Same as #await!, but raises a Timeout exception if the given t expires.


[View source]
def await! : T #

Awaits the completion of the future and either returns the computed value or raises an exception.


[View source]
def done? #

Returns true if the future has completed - either with a value or an exception. Returns false otherwise.


[View source]
def flat_map(&block : T -> Future(K)) : Future(K) forall K #

Creates a new future by applying a function to the successful result of this future, and returns the result of the function as the new future.

If this future is completed with an exception then the new future will also contain this exception.


[View source]
def flatten #

Creates a new future with one level of nesting flattened.


[View source]
def map(&block : T -> K) : Future(K) forall K #

Creates a new future by applying a function to the successful result of this future.

If this future is completed with an exception then the new future will also contain this exception.


[View source]
def map_to(typ : Object.class) #

Creates a new Future which is completed with this Future's result if that conforms to type typ or a TypeCastError otherwise.


[View source]
def on_complete(&block : T | Exception -> _) : Future(T) #

Applies the side-effecting function to the result of this future, and returns a new future with the result of this future.

This method allows one to enforce that the callbacks are executed in a specified order. Note: if one of the chained #on_complete callbacks throws an exception, that exception is not propagated to the subsequent #on_complete callbacks. Instead, the subsequent #on_complete callbacks are given the original value of this future.


[View source]
def on_error(&block : Exception -> _) : Future(T) #

Applies the side-effecting function to the result of this future if it raised an error, and returns a new future with the result of this future.

WARNING Will not be called if this future is never completed or if it is completed with success.


[View source]
def on_success(&block : T -> _) : Future(T) #

Applies the side-effecting function to the result of this future if it was successful, and returns a new future with the result of this future.

WARNING Will not be called if this future is never completed or if it is completed with an error.


[View source]
def recover(&block : Exception -> T) : Future(T) #

Creates a new future that will handle any matching throwable that this future might contain. If there is no match, or if this future contains a valid result then the new future will contain the same.


[View source]
def select(&block : T -> Bool) : Future(T) #

Creates a new future by filtering the value of the current future with a predicate.

If the current future contains a value which satisfies the predicate, the new future will also hold that value. Otherwise, the resulting future will fail with a EmptyError. If the current future fails, then the resulting future also fails.


[View source]
def transform(&block : T | Exception -> K) : Future(K) forall K #

Creates a new Future by applying the specified function to the result of this Future.

If there is any non-fatal exception thrown when 'block' is applied then that exception will be propagated to the resulting future.


[View source]
def zip(other : Future(K), &block : T, K -> W) : Future(W) forall K, W #

Creates a new future holding the result of block applied to the tuple of values from two futures.


[View source]