Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Created January 30, 2021 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GauravChaddha1996/fce016f692a6c54084503289d457eab1 to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/fce016f692a6c54084503289d457eab1 to your computer and use it in GitHub Desktop.
suspend fun someTaskWrapped() = suspendCancellableCoroutine<Int> {
it.invokeOnCancellation {
// Cancel any task or handle resources that you want
}
someTask(
object:Callback{
override fun onSuccess(result: Int) {
it.resume(result)
// Resume the calling coroutine with this result
}
override fun onFailure(e: Exception) {
it.resumeWithException(e)
// Resume the calling coroutine with an exception
// And let it handle the failure correctly
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment