如何触发 CompletableDeferred<String> 的 onAwait 函数?

How to trigger a function onAwait of a CompletableDeferred<String>?

我正在尝试在创建图像时对其进行索引。我认为这会很简单:

val saveLocation = CompletableDeferred<String>()
saveLocation.onAwait { loc:String ->
    MediaScannerConnection.scanFile(applicationContext, arrayOf(loc), null, null)
}

但是上面写着 Expression onAwait of type SelectClause1<String> cannot be invoked as a function 所以也许我用错了?有没有办法注册一个在 CompletableDeferred 完成时调用的函数?

onAwait 是 属性 其中 returns SelectClause1<T>

所以你会使用这样的东西

val clause = saveLocation.onAwait
clause.registerSelectClause1( // your args here)

但是,如果您想要延迟的结果,为什么不启动协程并调用 await

launch {
  val result = saveLocation.await()
  MediaScannerConnection.scanFile(applicationContext, arrayOf(result), null, null)
}