Created
April 5, 2018 16:20
-
-
Save nanlabsweb/d885ee1a28a904fcbeef0cb43713a07b to your computer and use it in GitHub Desktop.
Managing exceptions inside a coroutine.
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
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