tapac
10/20/2019, 6:47 PMDeferred
interface? I was trying to delegate that interface to an instance returned by async
function, but it doesn't work well.
class MyDeferred<T>(val original: Deferred<T>) : Deferred<T> by original
MyDeferred(async{ ...} )
I want to be able to call some code on async completition or execption, maybe there is another way?Adam Powell
10/20/2019, 7:08 PMasync {}
block, async {
try {
stuff()
} finally {
codeToRunOnCompletion()
}
}
try {
theDeferred.await()
} catch (e: TheExceptionThrown) {
doStuffWith(e)
}
since many things may .await()
on a Deferred and respond when the underlying async is donetapac
10/20/2019, 8:41 PMfun <T> executeAsync(body: ()->T) : MyDeferred<T> {
}
awaitAll
octylFractal
10/20/2019, 8:53 PMasync {}
block, do so.tapac
10/20/2019, 9:15 PMspecial
extension for MyDeferred
which will chain executions and execute additional code only once.
fun <T, R> MyDeferred<T>.and(body: (T) -> R) : MyDeferred<R>
octylFractal
10/20/2019, 9:40 PMsuspend fun <T, R> Deferred<T>.and(body: suspend (T) -> R) = async { body(await()) }
Dico
10/21/2019, 2:20 AMdeferred.invokeOnCompletion { .... }
map
and all those operators, you can indeed use Flow
or you can even use Java's CompletableFuture
. You can add the kotlinx-coroutines-jdk8
as a dependency and call Deferred.asCompletableFuture()
I think.