https://kotlinlang.org logo
#coroutines
Title
# coroutines
b

bogdoll

08/10/2019, 8:27 AM
For working non blocking the AWS2 SDK provides methods which returns CompleteableFuture<T>. Wrapping them into coroutines I wrote the following extension function (I don't like the name). My question would be, is that extension okay, or even already implemented in the standard coroutines library? If that extension is okay, what would you suggest as name for it?
Copy code
suspend fun <T> CompletableFuture<T>.suspending() : T = suspendCoroutine { cont ->
    whenComplete { v, e -> if(e==null) cont.resume(v) else cont.resumeWithException(e) }
}
It can be used for example to wrap the following AWS SDK method:
CompletableFuture<CopyObjectResponse> copyObject(CopyObjectRequest copyObjectRequest)
into
suspend fun S3AsyncClient.suspendingCopyObject(copyObjectRequest: CopyObjectRequest) : CopyObjectResponse = copyObject(copyObjectRequest).suspending()
s

SiebelsTim

08/10/2019, 8:29 AM
There is a library that contains await for CompletableFutute
b

bogdoll

08/10/2019, 8:33 AM
Cool, thanks.
d

Dico

08/10/2019, 9:26 AM
One potential improvement would be to make it cancellable, see await implementation in above library
4 Views