CancellableFuture

CancellableFuture is an object that for all practical uses works like a future but enables the user to cancel the operation. A cancelled future fails with CancellableFuture.CancelException so the subscriber can differentiate between thisand other failure reasons.

CancellableFuture is an object that for all practical uses works like a future but enables the user to cancel the operation. A cancelled future fails with CancellableFuture.CancelException so the subscriber can differentiate between thisand other failure reasons.

See also
Companion
class
class Object
trait Matchable
class Any

Type members

Classlikes

case object CancelException extends Exception with NoStackTrace

When the cancellable future is cancelled, CancelException is provided as the reason of failure of the underlying future.

When the cancellable future is cancelled, CancelException is provided as the reason of failure of the underlying future.

final class RichFuture[T](val future: Future[T]) extends AnyVal

Value members

Concrete methods

@inline
def apply[T](body: => T)(ec: ExecutionContext): CancellableFuture[T]

Creates CancellableFuture[T] from the given function with the result type of T.

Creates CancellableFuture[T] from the given function with the result type of T.

Type Params
T

The result type of the given function

Value Params
body

The function to be executed asynchronously

ec

The execution context

Returns

A cancellable future executing the function

Creates an already cancelled CancellableFuture[T].

Creates an already cancelled CancellableFuture[T].

Type Params
T

The type of what the future would return had it been successful

Returns

An already cancelled uncancellable future

def delay(duration: FiniteDuration)(ec: ExecutionContext): CancellableFuture[Unit]

Creates an empty cancellable future that will finish its execution with success after the given time. Typically used together with map or a similar method to execute computation with a delay.

Creates an empty cancellable future that will finish its execution with success after the given time. Typically used together with map or a similar method to execute computation with a delay.

Value Params
duration

The duration after which the future finishes its execution

ec

The execution context

Returns

A new cancellable future of Unit which will finish with success after the given time

def delayed[T](duration: FiniteDuration)(body: => T)(ec: ExecutionContext): CancellableFuture[T]

A utility method that combines delay with map. Creates a cancellable future that will execute the given body block after the given time.

A utility method that combines delay with map. Creates a cancellable future that will execute the given body block after the given time.

Type Params
T

the type of the result returned by body

Value Params
body

A block of code which will be run after the duration

duration

The duration after which the future finishes its execution

ec

The execution context

Returns

A new cancellable future of T

def failed[T](th: Throwable): CancellableFuture[T]

Creates an already failed CancellableFuture[T] with the given throwable as the failure reason.

Creates an already failed CancellableFuture[T] with the given throwable as the failure reason.

Type Params
T

The type of what the future would return had it been successful

Value Params
th

A Throwable being the reason of why the future is failed

Returns

An already failed uncancellable future

@inline
def from[T](promise: Promise[T])(ec: ExecutionContext): CancellableFuture[T]

Turns a Promise[T] into a cancellable CancellableFuture[T].

Turns a Promise[T] into a cancellable CancellableFuture[T].

Type Params
T

The promise's result type

Value Params
ec

The execution context

promise

The promise to be lifted

Returns

A new cancellable future wrapped over the original promise

@inline
def lift[T](future: Future[T])(ec: ExecutionContext): CancellableFuture[T]

Turns a regular Future[T] into an uncancellable CancellableFuture[T].

Turns a regular Future[T] into an uncancellable CancellableFuture[T].

Type Params
T

The future's result type

Value Params
ec

The execution context

future

The future to be lifted

Returns

A new uncancellable future wrapped over the original future

def repeat(duration: Duration)(body: => Unit)(ec: ExecutionContext): CancellableFuture[Unit]

Creates an empty cancellable future which will repeat the mapped computation every given duration until cancelled. The first computation is executed with duration delay. If the operation takes longer than duration it will not be cancelled after the given time, but also the ability to cancel it will be lost.

Creates an empty cancellable future which will repeat the mapped computation every given duration until cancelled. The first computation is executed with duration delay. If the operation takes longer than duration it will not be cancelled after the given time, but also the ability to cancel it will be lost.

Value Params
body

A task repeated every duration.

duration

The initial delay and the consecutive time interval between repeats.

Returns

A cancellable future representing the whole repeating process.

def sequence[T](futures: Iterable[CancellableFuture[T]])(ec: ExecutionContext): CancellableFuture[Iterable[T]]

Creates a new CancellableFuture[Iterable[T]] from a Iterable of Iterable[T]. The original futures are executed asynchronously, but there are some rules that control them:

Creates a new CancellableFuture[Iterable[T]] from a Iterable of Iterable[T]. The original futures are executed asynchronously, but there are some rules that control them:

  1. All original futures need to succeed for the resulting one to succeed.
  2. Cancelling the resulting future will cancel all the original ones except those that are uncancellable. 3a. If one of the original futures is cancelled (if it's cancellable) the resulting future will be cancelled and all other original futures (those that are not uncancellable) will be cancelled as well. 3b. If one of the original futures fails for any other reason than cancellation, the resulting future will fail with the same reason as the original one and all other original futures will be cancelled (if they are not uncancellable) - i.e. they will not fail with the original reason but with CancelException.
Type Params
T

The type returned by each of original cancellable futures

Value Params
ec

The execution context

futures

A collection (Iterable) of cancellable futures of the same type T

Returns

A cancellable future with its result being a collection of results of original futures in the same order

def successful[T](result: T): CancellableFuture[T]

Creates an already completed CancellableFuture[T] with the specified result.

Creates an already completed CancellableFuture[T] with the specified result.

Type Params
T

The type of the result

Value Params
result

The result of the completed future

Returns

An already successfully completed uncancellable future

@inline
def toUncancellable[T](futures: CancellableFuture[T]*): Iterable[CancellableFuture[T]]

Turns one or more of cancellable futures into "uncancellable" - entities of the Uncancellable subsclass of CancellableFuture.

Turns one or more of cancellable futures into "uncancellable" - entities of the Uncancellable subsclass of CancellableFuture.

Type Params
T

The type of the result of the original cancellable futures

Value Params
futures

One or more cancellable futures you want to make sure won't get cancelled

Returns

A collection of copies of cancellable futures which are uncancellable

See also

Uncancellable for details

def traverse[T, U](in: Iterable[T])(f: T => CancellableFuture[U])(ec: ExecutionContext): CancellableFuture[Iterable[U]]

Transforms an Iterable[T] into a CancellableFuture[Iterable[U]] using the provided function T => CancellableFuture[U]. Each cancellable future will be executed asynchronously. If any of the original cancellable futures fails or is cancelled, the resulting one will will fail immediately, but the other original ones will not.

Transforms an Iterable[T] into a CancellableFuture[Iterable[U]] using the provided function T => CancellableFuture[U]. Each cancellable future will be executed asynchronously. If any of the original cancellable futures fails or is cancelled, the resulting one will will fail immediately, but the other original ones will not.

Type Params
T

The type of input elements

U

The type of the result of cancellable futures created by f

Value Params
ec

The execution context

f

A function that for each input element will create a new cancellable future of the type U

in

A collection (Iterable) of input elements of the same type T

Returns

A cancellable future with its result being a collection of results of futures created by f

See also

sequence for cancellation rules

def traverseSequential[T, U](in: Iterable[T])(f: T => CancellableFuture[U])(ec: ExecutionContext): CancellableFuture[Iterable[U]]

Transforms an Iterable[T] into a CancellableFuture[Iterable[U]] using the provided function T => CancellableFuture[U]. Each cancellable future will be executed synchronously. If any of the original cancellable futures fails or is cancelled, the resulting one also fails and no consecutive original futures will be executed.

Transforms an Iterable[T] into a CancellableFuture[Iterable[U]] using the provided function T => CancellableFuture[U]. Each cancellable future will be executed synchronously. If any of the original cancellable futures fails or is cancelled, the resulting one also fails and no consecutive original futures will be executed.

Cancelling the resulting future will cancel the one which is executed at the moment and it will prevent all the consecutive original futures from being executed.

Type Params
T

The type of input elements

U

The type of the result of cancellable futures created by f

Value Params
ec

The execution context

f

A function that for each input element will create a new cancellable future of the type U

in

A collection (Iterable) of input elements of the same type T

Returns

A cancellable future with its result being a collection of results of futures created by f

See also

sequence for cancellation rules

def zip[T, U](f1: CancellableFuture[T], f2: CancellableFuture[U])(ec: ExecutionContext): CancellableFuture[(T, U)]

Creates a new CancellableFuture[(T, U)] from two original cancellable futures, one of the type T, and one of the type U. The new future will fail or be cancelled if any of the original ones fails. Cancelling the new future will fail both original ones.

Creates a new CancellableFuture[(T, U)] from two original cancellable futures, one of the type T, and one of the type U. The new future will fail or be cancelled if any of the original ones fails. Cancelling the new future will fail both original ones.

Type Params
T

The type of the result of the first of original cancellable futures

U

The type of the result of the second of original cancellable futures

Value Params
ec

The execution context

f1

The first of two original cancellable futures, with the result of type T

f2

The second of two original cancellable futures, with the result of type U

Returns

A new cancellable future with its result being a tuple of results of both original futures

Implicits

Implicits

implicit def toFuture[T](f: CancellableFuture[T]): Future[T]