Kotlin Coroutines with returning value

前端 未结 6 875
盖世英雄少女心
盖世英雄少女心 2020-12-17 08:54

I want to create a coroutine method which has returning value.

For example)

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

fun funB() = async(Commo         


        
6条回答
  •  不知归路
    2020-12-17 09:19

    I edit your work, i change the funA and funB into a suspend function and i created a function for sum operator and i call on main function, this the example :

    suspend fun funA(): Int{
        return 1
    }
    
    suspend fun funB(): Int {
        return 2
    }
    fun sum() = runBlocking{
        val resultSum = async { funA.await() + funB.await() }
        return resultSum
    }
    
    fun main() = runBlocking{
        val result = async { sum() }
        println("Your result: ${result.await()}")
    }
    

    Hope it will help

提交回复
热议问题