Kotlin是否在参数中按顺序等待?

Kotlin是否在参数中按顺序等待?,kotlin,kotlin-coroutines,Kotlin,Kotlin Coroutines,科特林是否严格按顺序等待 接受者,等待 等待 第7行: suspend fun sendEmail(r: String, msg: String): Boolean { //(6) delay(2000) println("Sent '$msg' to $r") return true } suspend fun getReceiverAddressFromDatabase(): String { //(4) d

科特林是否严格按顺序等待

接受者,等待 等待 第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 "coroutine@kotlin.org"
    }

    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)
    }
或者以任何顺序?我的意思是Kotlin是否首先检查收件人。等待,并且只有在它完成后才检查消息。等待?

您的问题不是等待的具体问题,而是深入到Kotlin本身的评估顺序语义

所以,这里有一段引用自:

然后,函数参数将按照其在函数调用中从左到右的出现顺序进行求值

应用于您的代码行:

sendEmail(recipient.await(), msg.await())
很明显,首先计算recipient.await,然后计算msg.await。等待调用直到问题中的延迟完成后才会完成。

您的问题不是等待特定的问题,而是更深入的问题,具体到Kotlin本身的求值顺序语义

所以,这里有一段引用自:

然后,函数参数将按照其在函数调用中从左到右的出现顺序进行求值

应用于您的代码行:

sendEmail(recipient.await(), msg.await())
很明显,首先计算recipient.await,然后计算msg.await。等待呼叫在相关延迟完成之前不会完成