Endre Deak
11/03/2021, 7:11 PMsuspend fun callback() { ... }
suspend fun execute(request: Request): Response {
val response = ...
callback()
return response
}
streetsofboston
11/03/2021, 7:43 PMfun CoroutineScope.execute(request: Request, callback: suspend () -> Unit) {
val response: Response = ...
launch { callback() }
return response
}
Endre Deak
11/03/2021, 8:33 PMsuspend fun execute(request: Request, callback: suspend () -> Unit): Response = withContext(Dispatchers.Default) {
val response : Response = ...
launch { callback() }
response
}
Nick Allen
11/03/2021, 11:23 PMwithContext
will wait for all child coroutines to finish (read up on structured concurrency for why).
In general it's not a great idea to have a suspend function that returns before it's finished all the work it started. Usually a method is either suspend or it takes a CoroutineScope somehow, usually not both.
What warnings in ide did you get? Is the ...
stuff suspending code?val job = Job(); job.join()
) and check that the other function returns without giving the signal (job.complete()
).Endre Deak
11/04/2021, 6:09 AM