halim
04/28/2019, 9:19 AMi am confuse with this syntax
fun task(bloc: suspend () -> Unit) { .... }
what does mean call suspend function inside regular function ?octylFractal
04/28/2019, 9:21 AMbloc
a suspend
lambda, i.e. you need to call bloc
from a suspend function
it doesn't matter if the parameter is on a regular function, because the function isn't calling bloc
halim
04/28/2019, 9:22 AMsimon.vergauwen
04/28/2019, 9:22 AMstartCoroutine
with a callback.halim
04/28/2019, 9:23 AMsimon.vergauwen
04/28/2019, 9:23 AMhalim
04/28/2019, 9:24 AMsimon.vergauwen
04/28/2019, 9:25 AMoctylFractal
04/28/2019, 9:25 AMfun <T> runSomethingTwice(block: suspend () -> T ): T = runBlocking {
block()
block()
}
this function calls block()
, which may need to suspend (it uses some suspending resource) twice, using runBlocking
to provide a CoroutineScope / suspend contextsimon.vergauwen
04/28/2019, 9:25 AMoctylFractal
04/28/2019, 9:26 AMrunBlocking
to get such a context, but there are other options like async
and launch
halim
04/28/2019, 9:35 AMoctylFractal
04/28/2019, 9:38 AMlaunch
or async
, in which case it wonthalim
04/28/2019, 10:00 AMgildor
04/28/2019, 5:06 PMif wrap regular fun in scope with (async, launch) it becomes async-funIt's incorrect, function is still syncronous and cannot suspend
so how about implemenation of coroutine builder (launch, async, runbloking, flow)If you interested how it implemented check sources (or even better coroutines design reference which has simple veraions of those builders), under the hood they use low level coroutine builders provided by stdlib, which used to create coroutine from common callback based asyncronous code
halim
04/29/2019, 11:07 AMfun CoroutineScope.someCall() = launch {
println("inside some call")
}
main() = runblocking {
println("before")
someCall()
println("done")
}
when execute: before -> done -> inside some call
so when wrap regular fun (someCall) in coroutine scope it become asynchronegildor
04/29/2019, 11:08 AMhalim
04/29/2019, 11:10 AMgildor
04/29/2019, 11:11 AMhalim
04/29/2019, 11:12 AM