This is the current implementation and it waits ti...
# coroutines
a
This is the current implementation and it waits till SDK is done with submission, SDK.submitData is non cooperative in nature
Copy code
override suspend fun execute() {
    withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
        suspendCoroutine { cont ->
            SDK.submitData(
                object : Callback<SubmitResponse> {
                    override fun onSuccess(response: SubmitResponse?) {
                        cont.resume()
                    }

                    override fun onError(exception: Exception?) {
                        cont.resumeWithException(exception)
                    }
                }
            )
        }
        
    }
}
🧵 1
w
I believe that the problem is that you are using
suspendCoroutine
here instead of
suspendCancellableCoroutine
. I believe the latter should work
p
You could use CompletableDeferred to capture the results from your callback and await it with cancellation (given you don't want or can't have the cancellation propagate to the SDK):
Copy code
suspend fun execute(): SubmitResponse? {
    val result = CompletableDeferred<SubmitResponse?>()
    SDK.submitData(object : Callback<SubmitResponse> {
        override fun onSuccess(response: SubmitResponse?) {
            result.complete(response)
        }
        override fun onError(exception: Exception) {
            result.completeExceptionally(exception)
        }
    })
    
    return withTimeoutOrNull(500.milliseconds) { result.await() }
}
https://pl.kotl.in/HePwqeOF7
🙌 1