# ConsumerKt

Added in 1.8.0

*** ** * ** ***


```
public final class ConsumerKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 24) <T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/core/util/ConsumerKt#(kotlin.coroutines.Continuation).asConsumer()(@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-continuation/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> receiver)` Returns a `https://developer.android.com/reference/java/util/function/Consumer.html` that will resume this `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-continuation/index.html` when the result of an operation is `https://developer.android.com/reference/java/util/function/Consumer.html#accept(T)`. |

## Public methods

### asConsumer

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/util/PlatformConsumer.kt+function:asConsumer)

```
@RequiresApi(value = 24)
public static final @NonNull Consumer<@NonNull T> <T extends Object> asConsumer(@NonNull Continuation<@NonNull T> receiver)
```

Returns a `https://developer.android.com/reference/java/util/function/Consumer.html` that will resume this `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-continuation/index.html` when the result of an operation is `https://developer.android.com/reference/java/util/function/Consumer.html#accept(T)`.

Useful for writing `suspend` bindings to async methods that accept `https://developer.android.com/reference/java/util/function/Consumer.html` as a result callback for a one-time operation:

```
public suspend fun FancinessManager.query(
    query: FancinessManager.Query
): FancinessManager.QueryResult = suspendCancellableCoroutine<QueryResult> { continuation ->

    // Any Android API that supports cancellation should be configured to propagate
    // coroutine cancellation as follows:
    val canceller = CancellationSignal()
    continuation.invokeOnCancellation { canceller.cancel() }

    // Invoke the FancinessManager#queryAsync method as follows:
    queryAsync(
        query,
        canceller,
        // Use a direct executor to avoid extra dispatch. Resuming the continuation will
        // handle getting to the right thread or pool via the ContinuationInterceptor.
        Runnable::run,
        continuation.asConsumer()
    )
}
```