Micro Promise/A+ under 100 lines of code
Swift Ruby Objective-C
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Pill.xcodeproj Update project to recommended settings May 13, 2018
Sources Switch to ContiguousArray Dec 23, 2017
Supporting Files Version 0.4 Dec 23, 2017
Tests Add performance tests Dec 23, 2017
.gitignore Initial commit Jul 31, 2016
.swift-version Version 0.3 Oct 17, 2017
.travis.yml Fix tests Dec 23, 2017
CHANGELOG.md Version 0.4 Dec 23, 2017
LICENSE Initial commit Jul 31, 2016
Package.swift Update Copyright Oct 17, 2017
Pill.podspec Version 0.4 Dec 23, 2017
README.md Version 0.3 Oct 17, 2017

README.md

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.