相关疑难解决方法(0)

Kotlin Coroutines具有回归价值

我想创建一个具有返回值的协程方法.

例如)

fun funA() = async(CommonPool) {
    return 1
}

fun funB() = async(CommonPool) {
    return 2
}

fun sum() {
    launch {
        val total = funA().await() + funB().await()
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我想要求总和方法,我该怎么办?

喜欢,

fun sum(): Int {
    launch {
        val total = funA().await() + funB().await()
    }   

    return total
}
Run Code Online (Sandbox Code Playgroud)

kotlin kotlinx.coroutines

11
推荐指数
6
解决办法
1万
查看次数

标签 统计

kotlin ×1

kotlinx.coroutines ×1