Skip to content

jarroo/FuturePromise: Port of Apple's SwiftNIO implementation of Futures and Promises, based on DispatchQueue instead of EventLoop.

License

Notifications You must be signed in to change notification settings

aceontech/FuturePromise

Repository files navigation

FuturePromise, a Swift promises implementation for iOS, ported from Apple's own SwiftNIO project

Carthage compatible Cocoapods compatible

Introduction

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.

Goal

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.

Installation

Cocoapods

Add the following to your Podfile & run pod install:

pod 'FuturePromise'

Carthage

Add the following to your Cartfile & run carthage update:

github "jarrroo/FuturePromise"

Documentation

The generated documentation pages can be found here.

Usage examples

From Apple's SwiftNIO documentation:

doWork().then {
  doMoreWork($0)
}.then {
  doYetMoreWork($0)
}.thenIfError {
  maybeRecoverFromError($0)
}.map {
  transformData($0)
}.cascade(promise: userPromise)