Skip to content

Instantly share code, notes, and snippets.

@mochadwi
Last active June 23, 2021 16:09
Show Gist options
  • Save mochadwi/b4a8508c572465300526e9c0acd0ead2 to your computer and use it in GitHub Desktop.
Save mochadwi/b4a8508c572465300526e9c0acd0ead2 to your computer and use it in GitHub Desktop.
@JvmOverloads
fun <R> suspendFunction(
onSuccees: Consumer<R?>,
onFailure: Consumer<Throwable?>,
dispatcher: CoroutineDispatcher = Dispatchers.Default
): Continuation<R> = object : Continuation<R> {
override val context: CoroutineContext
get() = dispatcher
override fun resumeWith(result: Result<R>) {
if (result.isSuccess && result.isFailure.not()) {
onSuccees.accept(result.getOrNull())
} else {
onFailure.accept(result.exceptionOrNull())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment