Skip to content

Instantly share code, notes, and snippets.

@folkyatina
Created December 8, 2018 15:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save folkyatina/2b9e755a1aeaed8c27b1d31535d8a387 to your computer and use it in GitHub Desktop.
Save folkyatina/2b9e755a1aeaed8c27b1d31535d8a387 to your computer and use it in GitHub Desktop.
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