我正在尝试使用带有协程的 Futures 创建一个 Observable。
这是我尝试过的:
private fun getHelloObservable(): Observable<String>{
val deferred = GlobalScope.async {
"Hello"
}
return Observable.just(deferred.await())
}
但我收到以下错误:
Suspend function 'await' should be called only from a coroutine or another suspend function.
有办法做到这一点吗?
最佳答案
您可以使用 kotlinx-coroutines-rx2 来桥接响应式(Reactive)世界:
rxSingle { deferred.await() }
从这里开始,只需调用 toObservable()
即可轻松获取 Observable
。
关于kotlin - 如何使用 Kotlin 协程从 Deferred future 创建 Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210119/