-
-
Save mochadwi/b4a8508c572465300526e9c0acd0ead2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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