I have a suspend function which calls other 3 susp...
# coroutines
j
I have a suspend function which calls other 3 suspending functions. How can I have
doSomething
call the 3 suspending functions serially? (i.e. waiting for each one to complete before calling the other?)
Copy code
suspend fun doSomething() {
    suspendingFun1()
    suspendingFun2()
    suspendingFun3()
  }
🧵
Right now I came up with:
Copy code
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 🤷
r
@julioromano: Your first example should work. 🤔
8
Copy code
suspend fun doSomething() {
    val thing1 = suspendingFun1()
    val thing2 = suspendingFun2(thing1)
    suspendingFun3(thing2)
  }
s
I agree with @Richard Gomez. Your original example already calls all three of them sequentially. That is the beauty of suspend functions. They are asynchronous, but you can call them like they are regular functions, imperatively/sequentially.
j
True, I’ll have to double check what those functions do internally as I had the impression that running them like so yielded parallel network calls. Thanks for your input!
s
It is easier to visualize that they are called sequentially when using an example where they return a value and take input
Copy code
suspend fun doSomething(): Int {
    val res1 = suspendingFun1()
    val res2 = suspendingFun2(res1)
    return suspendingFun3(res2)
  }
k
If you need to lunch them sequentially then wait for all three of them to finish you’ve to modify your code as follows
Copy code
suspend fun doSomethingInSerially() = coroutineScope {
        val job1 = launch { suspendingFun1() }
        val job2 = launch { suspendingFun2() }
        val job3 = launch { suspendingFun3() }
        joinAll(job1, job2, job3)
    }
this would do what you need if you don’t expect a result from your async functions
r
@Kareem Waleed Wouldn't the use of
coroutineScope
implicitly wait for all of them to finish?
Copy code
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"
☝🏼 1
☝️ 2
👌 4
k
@Richard Gomez Yes, actually the use of
join()
in my example is redundant. sorry 😄
👍 2
👍🏼 1
m
the IDE is right. That is sequential:
Copy code
suspend fun doSomethingInSerially() {
    coroutineScope {
      async { suspendingFun1() }.await()
      async { suspendingFun2() }.await()
      async { suspendingFun3() }.await()
    }
  }