We've already met deferred values in Chapter 8, Threads and Coroutines, in the Returning results section. Deferred is the result of the async() function, for example. You may also know them as Futures from Java or Scala, or as Promises from JavaScript.
Interestingly enough, Deferred is a Proxy design pattern that we've met in previous chapters.
Much as the Kotlin Sequence is very similar to the Java8 Stream, Kotlin Deferred is very similar to Java Future. You'll rarely need to create your own Deferred. Usually, you would work with the one returned from async().
In cases where you do need to return a placeholder for a value that would be evaluated in the future, you can do it:
val deferred = CompletableDeferred<String>()
launch {
delay(100)
if (Random().nextBoolean()) {
deferred.complete("OK")
}
else {
deferred...