Skip to content

Instantly share code, notes, and snippets.

@Starmel
Created April 5, 2021 10:23
Show Gist options
  • Save Starmel/41d9bf9a1f7e2d3c2993997a5888ac9f to your computer and use it in GitHub Desktop.
Save Starmel/41d9bf9a1f7e2d3c2993997a5888ac9f to your computer and use it in GitHub Desktop.
fun <Element> AsyncResult.Companion.fromCoroutine(
source: suspend () -> Element
): AsyncResult<Element> {
return create { emitter ->
val job = CoroutineScope(defaultDispatcher).launch {
try {
val result = source.invoke()
emitter.onSuccess(result.makeShared())
} catch (error: Throwable) {
emitter.onError(error.makeShared())
}
}
emitter.disposable = {
job.cancel()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment