Pill

Micro Promise/A+ under 100 lines of code


Keywords
async, chaining, future, promise, result
License
MIT
Install
pod try Pill

Documentation

Micro Promise/A+ under 100 lines of code. Has all the essential features, adapted for Swift. Covered by Promise/A+ test suite.

Requirements

  • iOS 9.0 / watchOS 2.0 / OS X 10.11 / tvOS 9.0
  • Xcode 9
  • Swift 4

API

Promise/A+

Instead of a single promise.then(onFulfilled, onRejected) method Pill has a bunch of type-safe methods with the same functionality:

Equivalent to onFulfilled:

func then<U>(_ closure: @escaping (T) throws -> U) -> Promise<U>
func then<U>(_ closure: @escaping (T) throws -> Promise<U>) -> Promise<U>

Equivalent to onRejected:

func catch(_ closure: @escaping (Error) throws -> Void) -> Promise<T>
func recover(_ closure: @escaping (Error) throws -> T) -> Promise<T>
func recover(_ closure: @escaping (Error) throws -> Promise<T>) -> Promise<T>

Each of the then / catch methods also have an on queue: DispatchQueue parameter which is .main by default.

Additions:

func finally(_ closure: @escaping (Void) -> Void) -> Promise<T>

Creating Promises

let promise = Promise { fulfill, reject in
    doSomething { value, error in
        if let value = value {
            fulfill(value)
        } else {
            reject(error)
        }
    }.resume()
}

Already fulfilled:

let promise = Promise(value: 1)

Already rejected:

let promise = Promise<Int>(error: Error.unknown)

Synchronous Inspection

var isPending: Bool
var value: T?
var error: Error?

License

Pill is available under the MIT license. See the LICENSE file for more info.