从协程内部的外部函数返回-Kotlin

时间:2020-09-09 17:23:58

标签: label kotlin-coroutines

我想从协程内部的外部函数返回。以下是我的(失败)尝试。当我尝试返回时,出现编译时错误

此处不允许“返回”

有人知道解决方法吗?还是不可能?

fun main() {
    run func@ {
        GlobalScope.launch {
            withContext(Dispatchers.IO) {
                return@func // 'return' is not allowed here
            }
        }
        println("This line should not be executed")   
    }
}

0 个答案:

没有答案