且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将暂停函数作为参数传递给另一个函数? Kotlin协程

更新时间:2023-11-09 23:18:16

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()
    }
  }
}