在lambda内部运行协程

时间:2020-01-27 09:55:57

标签: kotlin-coroutines

在此Kotlin代码中:

async {
    lateinit var testResult: TestResult
    val startTime = Date()

    try {
        withContext(Dispatchers.IO) {
            unitTest.getTestToRunAsync()?.invoke { result ->
                testResult = result
                doOnTestCompleted(testInfo, testResult, startTime, Date())
            }
        }
    } catch (exception: Exception) {
        testResult = TestResult("Exception: ${exception.message}")
    }
}

我想调用doOnTestCompleted,其定义如下:

private suspend fun doOnTestCompleted(
    testInfo: TestInfo,
    testResult: TestResult,
    startTime: Date,
    endTime: Date,
    copyTestResults: Boolean = true
) {

}

但是我不能调用doOnTestCompleted,因为从其调用的lambda主体不是协程。 getTestToRunAsync的定义是:

fun getTestToRunAsync(): (suspend (testToRunAsync: (resultCallback: TestResult) -> Unit) -> Unit)? {
    return this.testToRunAsync
}

我怎么称呼doOnTestCompleted?

0 个答案:

没有答案