future

fun <T> CoroutineScope.future(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> T
): ListenableFuture<T>

Starts block in a new coroutine and returns a ListenableFuture pointing to its result.

The coroutine is immediately started. Passing CoroutineStart.LAZY to start throws IllegalArgumentException, because Futures don’t have a way to start lazily.

The created coroutine is cancelled when the resulting future completes successfully, fails, or is cancelled.

CoroutineContext is inherited from this CoroutineScope. Additional context elements can be added/overlaid by passing context.

If the context does not have a CoroutineDispatcher, nor any other ContinuationInterceptor member, Dispatchers.Default is used.

The parent job is inherited from this CoroutineScope, and can be overridden by passing a Job in context.

See newCoroutineContext for a description of debugging facilities.

Note that the error and cancellation semantics of future are subtly different than asListenableFuture’s. See ListenableFutureCoroutine for details.

Parameters

context - added overlaying CoroutineScope.coroutineContext to form the new context.

start - coroutine start option. The default value is CoroutineStart.DEFAULT.

block - the code to execute.