Future

Future

new Future(executor) → {Future}

Creates a Future that will get its outcome using the provided executor function.

Future's executors are invoked synchrously (immediately).

Parameters:
Name Type Description
executor executor
Returns:
Type:
Future

Methods

subscribe(onSuccessopt, onErroropt, onCancelopt) → {function}

Attach callbacks to be invoked once the Future is resolved/rejected/cancelled.

Returns a function that can be used to cancel the subscription.

Parameters:
Name Type Attributes Description
onSuccess function <optional>

callback invoked if the Future has succeded

onError function <optional>

callback invoked if the Future has aborted. If

onCancel function <optional>

callback invoked if the Future was cancelled

Returns:
Type:
function

a function that can be used to cancel the subscription.

fork()

Returns a new Future that will complete with the same outcome as the input Future.

Cancelling this Future will not cancel the original Future.

cancel(reason)

Cancels the Future with provided reason. Cancellation forces the outcome of this Future into a Cancelled state.

Cancellation will be notified to all subscribers that have provided an onCancel callback.

Parameters:
Name Type Description
reason *

then(onResolve, onReject, onCancel)

Chain a callback that will be invoked when this Future completes. The appropriate callback will be invoked (if provided) depending on the type of the completion (success, error or cancellation).

If the invoked callback returns a Future then the result Future will adopt its outcome. Returning a non Future value is the same as returning Future.resolve(value).

If the appropriate callback is omitted then the result Future will adopt the same outcome of this Future.

Cancelling the result Future will not cancel this Future or the one eventually returned from a callback. That is, the cancellation will only affect the outcome of the result Future. If you want to cancel the root Futures as well, you must combine your sequence in a Task using Task#then

Parameters:
Name Type Description
onResolve function
onReject function
onCancel function
Returns:

@Future

orElse(f2) → {Future}

Runs a race between 2 Futures. The result Future will resolve/reject with the first resolved/rejected Future. Otherwise it will be cancelled with the latest cancelled Future.

Cancelling the result Future will not cancel the 2 input futures. That is, the cancellation will only affect the outcome of the result Future. If you want to cancel also the input futures you should combine then in a single Task using {@Task#orElse}

Parameters:
Name Type Description
f2 Future
Returns:
Type:
Future

(static) of(value) → {Future}

Creates a Future that is resolved with the provided value

Parameters:
Name Type Description
value *
Returns:
Type:
Future

(static) resolve()

Same as Future.of

(static) reject(error) → {Future}

Creates a Future that is rejected with the provided error

Parameters:
Name Type Description
error *
Returns:
Type:
Future

(static) cancel(reason) → {Future}

Creates a Future that is cancelled with the provided reason

Parameters:
Name Type Description
reason *
Returns:
Type:
Future

(static) empty()

Creates a Future that never completes

(static) all(futures) → {Future}

Returns a Future that will resolve with an Array containing the values of all resolved Tasks. Othewise it will be rejected/cancelled with the first rejected/cancelled input Future.

Cancelling the result Future will not cancel the input Futures. Cancellation will only affect the outcome of the result Future. If you want to cancel also the input Futures you must combine them in a single Task using Task.all

Parameters:
Name Type Description
futures Array.<Future>
Returns:
Type:
Future

(static) race()

Runs a race between the input Futures. Returns a Future that will resolve/reject with the first resolved/rejected Future. Otherwise, it will be cancelled with the latest cancelled Future.

Cancellation of the result Future will only affect its own outcome. If you want to cancel also the input Futures you must combine the futures in a single Task using Task.race