Futures and promises under the hood

A good approach to understand how promises work is to look at a possible, but extremely simplified implementation. So, we could start with something like the following to represent our results and errors, and our generic callbacks:

public enum Result<Value> {    case success(Value)    case failure(Error)}enum SimpleError: Error {    case errorCause1    case errorCause2}typealias Callback<T> = (Result<T>) -> Void
The Result generic defined in the preceding snippet is a particular case of a so-called Either type. Usually, Either types are presented at the same time as Option types, since they are related. Indeed, while Option types can represent a value of a given type or the absence of any value, Either types can represent ...

Get Hands-On Design Patterns with Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.