Reputation: 3059
Does Kotlin await exactly strictly in order
at line 7:
suspend fun sendEmail(r: String, msg: String): Boolean { //(6)
delay(2000)
println("Sent '$msg' to $r")
return true
}
suspend fun getReceiverAddressFromDatabase(): String { //(4)
delay(1000)
return "[email protected]"
}
suspend fun sendEmailSuspending(): Boolean {
val msg = async(CommonPool) { //(3)
delay(500)
"The message content"
}
val recipient = async(CommonPool) { //(5)
getReceiverAddressFromDatabase()
}
println("Waiting for email data")
val sendStatus = async(CommonPool) {
sendEmail(recipient.await(), msg.await()) //(7)
}
return sendStatus.await() //(8)
}
or in any order? I mean does Kotlin check first for recipient.await() and only after it completes go for check for msg.await()?
Upvotes: 1
Views: 225
Reputation: 200168
Your question isn't await
-specific and cuts much deeper, down to the evaluation order semantics of Kotlin itself.
So, here's a quote from the Kotlin language specification:
Function arguments are then evaluated in the order of their appearance in the function call left-to-right
Applied to your line of code:
sendEmail(recipient.await(), msg.await())
it is clear that first recipient.await()
is evaluated and then msg.await()
. The await()
call doesn't complete until the Deferred
in question has been completed.
Upvotes: 3