Created
December 8, 2018 15:29
-
-
Save folkyatina/2b9e755a1aeaed8c27b1d31535d8a387 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
suspend inline fun <reified T> Task<T>.await(): T = suspendCoroutine { | |
addOnCompleteListener { task -> | |
if (task.isSuccessful) { | |
// Black magic to make it return Unit instead of null | |
if (Unit is T) { | |
it.resume(Unit) | |
} else { | |
it.resume(task.result!!) | |
} | |
return@addOnCompleteListener | |
} | |
task.exception?.apply { it.resumeWithException(this) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment