CLOVIS
04/01/2022, 8:37 PMval task = scope.async {
foo()
}
suspend fun bar() {
// All calls require the task to be over,
// however only the first one(s) will really suspend,
// since it will be ready for the others
val foo = task.await()
doSomething(foo)
}
The problem is that this requires a scope
to be available when the task is instantiated. Since I do not care whether the task starts when it is instantiated or when the first call to bar
is made, is there a way to adapt this pattern without requiring the scope
?Paul Woitaschek
04/01/2022, 9:49 PMstreetsofboston
04/02/2022, 1:30 AMtask
a lambda taking a CoroutineSccope as a param and returning a Deferred.
val task: (CoroutineScope) -> Deferred<X> = { scope -> async { foo() } }
Then in bar() you can call it like this:
val foo: X = coroutineScope { task(this).await() }
Gabriel Feo
04/02/2022, 2:47 PMCLOVIS
04/07/2022, 5:16 PM