Skip to content

Instantly share code, notes, and snippets.

@nanlabsweb
Created April 5, 2018 16:20
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 nanlabsweb/d885ee1a28a904fcbeef0cb43713a07b to your computer and use it in GitHub Desktop.
Save nanlabsweb/d885ee1a28a904fcbeef0cb43713a07b to your computer and use it in GitHub Desktop.
Managing exceptions inside a coroutine.
suspend fun <T> Task<T>.await(): T = suspendCoroutine { continuation ->
addOnCompleteListener { task ->
when {
(task.result as QuerySnapshot).metadata.isFromCache ->
continuation.resumeWithException(Exception("without internet"))
task.isSuccessful -> continuation.resume(task.result)
else -> continuation.resumeWithException(task.exception!!)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment