Continuation

Kotlin|Java

public interfaceContinuation<TResult, TContinuationResult>


A function that is called to continue execution after completion of aTask.

Parameters
<TResult>

the Task's result type

<TContinuationResult>

the Continuation's result type

Summary

Public methods

abstract TContinuationResult
then(@NonNullTask<TResult> task)

Returns the result of applying this Continuation totask.

Public methods

then

abstract TContinuationResult then(@NonNullTask<TResult> task)

Returns the result of applying this Continuation totask.

To propagate failure from the completed Task callgetResult and allow theRuntimeExecutionException to propagate. The RuntimeExecutionException will be unwrapped such that the Task returned bycontinueWith orcontinueWithTask fails with the original exception.

To suppress specific failures callgetResult and catch the exception types of interest:

task.continueWith(newContinuation<String,String>(){@OverridepublicStringthen(Task<String>task){try{returntask.getResult(IOException.class);}catch(FileNotFoundExceptione){return"Not found";}catch(IOExceptione){return"Read failed";}}}

To suppress all failures guard any calls togetResult withisSuccessful:

task.continueWith(newContinuation<String,String>(){@OverridepublicStringthen(Task<String>task){if(task.isSuccessful()){returntask.getResult();}else{returnDEFAULT_VALUE;}}}
Parameters
@NonNullTask<TResult> task

the completed Task. Never null

Throws
java.lang.Exception

if the result couldn't be produced

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-10-31 UTC.