In March 2018, Apple released and open-sourced its SwiftNIO project, which is, in Apple's own words
"A cross-platform asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients"
While this framework is primarily geared toward Swift on the (Linux) server, I noticed it contained an implementation of
Futures and Promises. SwiftNIO (NIO = "Non-blocking Input Ouput") and its Promises implementation are based on
EventLoops for concurrency, but it was straightforward to port it to use DispatchQueues instead.
This Github repo is the result of that effort.
The goal of this project is to maintain an API-compatible port of SwiftNIO's Future and Promise implementation for use in
iOS (and macOS) projects, until Swift gets
a superior form of concurrency,
i.e. coroutines, or async await.
Until then, SwiftNIO's implementation of Promises can be considered Apple's first and only santioned implementation of the well-known concurrency pattern for Apple platforms.
Add the following to your Podfile & run pod install:
pod 'FuturePromise'Add the following to your Cartfile & run carthage update:
github "jarrroo/FuturePromise"The generated documentation pages can be found here.
From Apple's SwiftNIO documentation:
doWork().then {
doMoreWork($0)
}.then {
doYetMoreWork($0)
}.thenIfError {
maybeRecoverFromError($0)
}.map {
transformData($0)
}.cascade(promise: userPromise)