julioromano
08/05/2021, 11:02 AMdoSomething
call the 3 suspending functions serially? (i.e. waiting for each one to complete before calling the other?)
suspend fun doSomething() {
suspendingFun1()
suspendingFun2()
suspendingFun3()
}
🧵suspend fun doSomethingInSerially() {
coroutineScope {
async { suspendingFun1() }.await()
async { suspendingFun2() }.await()
async { suspendingFun3() }.await()
}
}
But I don’t know if it’s correct. And the IDE suggest me all those async {}.await()
calls are redundant and to replace them with withContext(Dispatchers.Default) {}
instead 🤷Richard Gomez
08/05/2021, 11:04 AMsuspend fun doSomething() {
val thing1 = suspendingFun1()
val thing2 = suspendingFun2(thing1)
suspendingFun3(thing2)
}
streetsofboston
08/05/2021, 11:40 AMjulioromano
08/05/2021, 11:50 AMstreetsofboston
08/05/2021, 12:12 PMsuspend fun doSomething(): Int {
val res1 = suspendingFun1()
val res2 = suspendingFun2(res1)
return suspendingFun3(res2)
}
Kareem Waleed
08/05/2021, 12:51 PMsuspend fun doSomethingInSerially() = coroutineScope {
val job1 = launch { suspendingFun1() }
val job2 = launch { suspendingFun2() }
val job3 = launch { suspendingFun3() }
joinAll(job1, job2, job3)
}
Richard Gomez
08/05/2021, 1:05 PMcoroutineScope
implicitly wait for all of them to finish?
suspend fun doSomethingInSerially() = coroutineScope {
launch { suspendingFun1() }
launch { suspendingFun2() }
launch { suspendingFun3() }
}
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html
"This function returns as soon as the given block and all its children coroutines are completed"Kareem Waleed
08/05/2021, 3:32 PMjoin()
in my example is redundant. sorry 😄myanmarking
08/06/2021, 11:09 AMsuspend fun doSomethingInSerially() {
coroutineScope {
async { suspendingFun1() }.await()
async { suspendingFun2() }.await()
async { suspendingFun3() }.await()
}
}