Deferred represents a pending operation’s result. When you call
deferred.await(), it waits for the end of the corresponding async operation to finish (it effectively suspends the current coroutine), and then either returns the operation’s return value, or throws the exception that the async operation has thrown (if any).
I don’t know the details of
Result, but I believe its purpose is to represent a finished operation’s result. Getting information out of it will return immediately with either the success or failure data.
Actually, there is a use case in the proposal for
Result itself where
Result are used together. You can see there that
Result is used to replace the exception-throwing behaviour by an object representation (using
Deferred is used to keep a reference to a running job.
You don’t really need Result in a synchronous method, though. Synchronous methods can just throw an exception, whereas async methods can’t.
Well you can also throw exceptions from async operations using
Deferred.await() mentioned above, since what matters is the moment we use the result of the async operation anyway.
Result is really about replacing/wrapping exceptions in cases where it’s more comfortable to use. And this includes some synchronous behaviours as well, like error aggregation on operations that process collections for instance.