Continuation

public interface Continuation<TResult, TContinuationResult>


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

Parameters
<TResult>

the Task's result type

<TContinuationResult>

the Continuation's result type

Summary

Public methods

abstract TContinuationResult
then(@NonNull Task<TResult> task)

Returns the result of applying this Continuation to task.

Public methods

then

abstract TContinuationResult then(@NonNull Task<TResult> task)

Returns the result of applying this Continuation to task.

To propagate failure from the completed Task call getResult and allow the RuntimeExecutionException to propagate. The RuntimeExecutionException will be unwrapped such that the Task returned by continueWith or continueWithTask fails with the original exception.

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

task.continueWith(new Continuation<String, String>() {
    @Override
    public String then(Task<String> task) {
        try {
            return task.getResult(IOException.class);
        } catch (FileNotFoundException e) {
            return "Not found";
        } catch (IOException e) {
            return "Read failed";
        }
    }
}

To suppress all failures guard any calls to getResult with isSuccessful:

task.continueWith(new Continuation<String, String>() {
    @Override
    public String then(Task<String> task) {
        if (task.isSuccessful()) {
            return task.getResult();
        } else {
            return DEFAULT_VALUE;
        }
    }
}
Parameters
@NonNull Task<TResult> task

the completed Task. Never null

Throws
java.lang.Exception

if the result couldn't be produced