Swiftpack.co - Package - koher/PromiseK

PromiseK

PromiseK provides a simple monadic Promise type for Swift.

// `flatMap` is equivalent to `then` of JavaScript's `Promise`
let a: Promise<Int> = asyncGet(2)
let b: Promise<Int> = asyncGet(3).map { $0 * $0 } // Promise(9)
let sum: Promise<Int> = a.flatMap { a in b.map { b in a + b } }

Promise can collaborate with throws for failable asynchronous operations.

// Collaborates with `throws` for error handling
let a: Promise<() throws -> Int> = asyncFailable(2)
let b: Promise<() throws -> Int> = asyncFailable(3).map { try $0() * $0() }
let sum: Promise<() throws -> Int> = a.flatMap { a in b.map { b in try a() * b() } }

It is also possible to recover from errors.

// Recovery from errors
let recovered: Promise<Int> = asyncFailable(42).map { value in
    do {
        return try value()
    } catch _ {
        return -1
    }
}

Installation

Swift Package Manager

.package(
    url: "https://github.com/koher/PromiseK.git",
    from: "3.0.0"
)

Carthage

github "koher/PromiseK" ~> 3.0.0

License

The MIT License

References

  1. Promise - JavaScript | MDN
  2. JavaScript Promises: There and back again - HTML5 Rocks
  3. A Fistful of Monads - Learn You a Haskell for Great Good!

Github

link
Stars: 43
Help us keep the lights on

Dependencies

Used By

Total:

Releases

3.1.1 - Sep 25, 2018

  • Xcode 10 / Swift 4.2
  • Supports macOS, tvOS and watchOS

#6

3.1.0 - Apr 2, 2018

3.0.0 - Apr 2, 2018

  • New simplified APIs
  • Supported Swift 4.0

3.0.0-alpha.4 - Oct 18, 2017

Now multiple fulfill calls cause a logic failure.

3.0.0-alpha.3 - Oct 18, 2017

- init(_ executor: (_ resolve: @escaping (Promise<Value>) -> ()) -> ())
+ init(_ executor: (_ fulfill: @escaping (Value) -> ()) -> ()) {