Futures and promises

Futures and promises are terms that are used somewhat interchangeably to refer to abstractions representing the idea of a proxy to a value that will be known at some future time. In other words, futures and promises allow you to create objects that encapsulate asynchronous tasks and their result, and use them without waiting for the asynchronous task to finish, as long as you do not need to access the result of that task. When you need to access the task result, either the task has already completed, in which case you can use it without delay; otherwise, you will need to delay the code that wants to use that result until it becomes available. Strictly speaking, a way to differentiate between futures and promises is to say that a future represents a read-only value that may be available or not, while a promise represents the function that is responsible to set that value at some point in time. Thus, in actual fact, futures and promises are two sides of the same coin. The act of setting the value of a future is called resolving the future or fulfilling the promise

Futures and promises originated in functional programming as a way to decouple values and computations, so you could associate one of multiple available promises (that is, ways of calculating a value) to a future. 

Futures and promises foster a direct style of handling concurrent operations. For example, our previous callbacks-based code could be rewritten as follows:

getListOfItems(url: url)
.onSuccess { item in useImage(item: item.itemUrl) }
.onFailure { item in useImage(item: item.itemUrl) }
.onFailure { useDefaultImage() }

As you can see, we get huge improvements in terms of the readability of our code, and can easily extend the promise chain to handle arbitrarily complex sequences of asynchronous operations.