is it ok to use ```continuation.isCompleted``` i ...
# coroutines
k
is it ok to use
Copy code
continuation.isCompleted
i have used this and its working fine, is it ok ?
Copy code
suspend fun <T> Task<T>.await(): T {
    return suspendCancellableCoroutine { continuation ->
        addOnSuccessListener {
            if (continuation.isCompleted.not())
                continuation.resume(it)
        }.addOnFailureListener {
            if (continuation.isCompleted.not())
                continuation.resumeWithException(it)
        }
    }
}
m
If this is the solution that you go for you could extract it into an extension function like
private fun <T> CancellableContinuation<T>.resumeOnlyOnce(value: T) {
if(!isCompleted) resume(value)
}
k
i'm not sure here if
isCompleted
working as i'm expecting, i have checked its not throwing an error, but i need confirmation for this
d
I think
isCompleted
doesn't indicate whether it was started reliably.
b
isCompleted
just informs you if it will no longer execute.
!isActive && !isCancelled && isCompleted
would both need to be checked that it was completed normally. You can figure out which combination(s) you need to check by referencing the states table on the Job docs https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/
d
I'm alluding to the ability to check if a continuation was already resumed. The best way might be to just try/catch