# CallbackToFutureAdapter

Artifact: [androidx.concurrent:concurrent-futures](https://developer.android.com/jetpack/androidx/releases/concurrent) [View Source](https://cs.android.com/search?q=file:androidx/concurrent/futures/CallbackToFutureAdapter.java+class:androidx.concurrent.futures.CallbackToFutureAdapter) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/concurrent#1.0.0)

*** ** * ** ***

<br />

```
public final class CallbackToFutureAdapter
```

<br />

*** ** * ** ***

A utility useful for adapting interfaces that take callbacks into interfaces that return `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html`.

It also provides additional safety checks, failing the future if it will never complete.

For example, you work with the following async api:

```
class AsyncApi  {
    interface OnResult {
        void onSuccess(Foo foo);
        void onError(Failure failure);
    }

    void load(OnResult onResult) {}
}
```

Code that wraps it as `ListenableFuture` would look like:

```
ListenableFuture<Foo> asyncOperation() {
    return CallbackToFutureAdapter.getFuture(completer -> {
        asyncApi.load(new OnResult() {
            @Override
            public void onSuccess(Foo foo) {
                completer.set(foo);
            }

            @Override
            public void onError(Failure failure) {
                completer.setException(failure.exception);
            }
        });
        // This value is used only for debug purposes: it will be used in toString()
        // of returned future or error cases.
        return "AsyncApi.load operation";
    });
}
```

Try to avoid creating references from listeners on the returned `Future` to the `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer` or the passed-in `tag` object, as this will defeat the best-effort early failure detection based on garbage collection.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer<T>` Used to complete the future returned by `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter#getFuture(androidx.concurrent.futures.CallbackToFutureAdapter.Resolver<T>)` |
| `public interface https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Resolver<T>` This interface should be implemented by the object passed into `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter#getFuture(androidx.concurrent.futures.CallbackToFutureAdapter.Resolver<T>)`. |

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<T>` | `<T> https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter#getFuture(androidx.concurrent.futures.CallbackToFutureAdapter.Resolver<T>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Resolver<T> callback)` Returns a Future that will be completed by the `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer` provided in `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Resolver#attachCompleter(androidx.concurrent.futures.CallbackToFutureAdapter.Completer<T>)`. |

## Public methods

### getFuture

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/concurrent#1.0.0)

```
public static @NonNull ListenableFuture<T> <T> getFuture(@NonNull CallbackToFutureAdapter.Resolver<T> callback)
```

Returns a Future that will be completed by the `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer` provided in `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Resolver#attachCompleter(androidx.concurrent.futures.CallbackToFutureAdapter.Completer<T>)`.

The provided callback is invoked immediately inline. Any exceptions thrown by it will fail the returned `Future`.