如何将暂停函数作为参数传递给另一个函数? Kotlin协程 [英] how to pass suspend function as parameter to another function? Kotlin Coroutines
本文介绍了如何将暂停函数作为参数传递给另一个函数? Kotlin协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将暂停函数作为参数发送,但是它显示修饰符'suspend'不适用于'value parameter'.怎么做?
I want to send suspending function as a parameter, but it shows " Modifier 'suspend' is not applicable to 'value parameter" . how to do it?
fun MyModel.onBG(suspend bar: () -> Unit) {
launch {
withContext(Dispatchers.IO) {
bar()
}
}
}
推荐答案
Lambda的suspend
修饰符应放在冒号后面,而不是前面.示例:
Lambda's suspend
modifier should be placed after the colon character, not in front. Example:
fun MyModel.onBG(bar: suspend () -> Unit) {
launch {
withContext(Dispatchers.IO) {
bar()
}
}
}
这篇关于如何将暂停函数作为参数传递给另一个函数? Kotlin协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文