We have recently introduced Coroutines to our codebase and have a method similar to the signature below for one of the asynchronous operations we need to perform:
private fun fetchSomethingFromServerAndReturnItsLocalPath(somethingId: String): Deferred<Result<File>>
We are experiencing problems with this approach when the
Result is set to indicate failure in the resulting
async Coroutine this method runs. It appears that when the Coroutine returns the exception is thrown rather the
Deferred<Result> object being passed onto the parent Coroutine as we had intended.
We have tried various solutions from searching online such as using a Supervisor Job or attempting to catch the exception when we call
await on our Deferred object but with no success so far.
As yet we haven’t found any mention online of returning a Result within a Deferred object however and so wondered if doing so is a suitable approach for this kind of operation? Or are there alternative implementations anyone could suggest that would allow us to return either the successful path of the item downloaded, or the failure case?
One thought is that we could provide our own
sealed class to essentially mimic the use of
Result here but we wondered if there was a better alternative to this.
Thanks in advance for any suggestions!