raphaelcruzeiro / PromiseKit

Promises for Swift & ObjC

Home Page:http://promisekit.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PromiseKit

badge-pod badge-languages badge-pms badge-platforms badge-mit

Modern development is highly asynchronous: isn’t it about time we had tools that made programming asynchronously powerful, easy and delightful?

UIApplication.sharedApplication().networkActivityIndicatorVisible = true

firstly {
    when(NSURLSession.GET(url).asImage(), CLLocationManager.promise())
}.then { image, location -> Void in
    self.imageView.image = image
    self.label.text = "\(location)"
}.always {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}.error { error in
    UIAlertView(/*…*/).show()
}

PromiseKit is a thoughtful and complete implementation of promises for any platform with a swiftc, it has excellent Objective-C bridging and delightful specializations for iOS, macOS, tvOS and watchOS.

Quick Start

# CocoaPods
pod "PromiseKit", "~> 3.4"

# Carthage
github "mxcl/PromiseKit" ~> 3.4

# SwiftPM
let package = Package(
    dependencies: [
        .Package(url: "https://github.com/mxcl/PromiseKit", majorVersion: 4)
    ]
)

Alternatively, drop PromiseKit.xcodeproj into your project and add PromiseKit.framework to your app’s embedded frameworks.

Documentation

We have thorough and complete documentation at promisekit.org.

Overview

Promises are defined by the function then:

login().then { json in
    //…
}

They are chainable:

login().then { json -> Promise<UIImage> in
    return fetchAvatar(json["username"])
}.then { avatarImage in
    self.imageView.image = avatarImage
}

Errors cascade through chains:

login().then {
    return fetchAvatar()
}.then { avatarImage in
    //…
}.catch { error in
    UIAlertView(/*…*/).show()
}

They are composable:

let username = login().then{ $0["username"] }

when(username, CLLocationManager.promise()).then { user, location in
    return fetchAvatar(user, location: location)
}.then { image in
    //…
}

They are trivial to refactor:

func avatar() -> Promise<UIImage> {
    let username = login().then{ $0["username"] }

    return when(username, CLLocationManager.promise()).then { user, location in
        return fetchAvatar(user, location: location)
    }
}

Continue Learning…

Complete and progressive learning guide at promisekit.org.

PromiseKit vs. Xcode

PromiseKit contains Swift, so we engage in an unending battle with Xcode:

Xcode Swift PromiseKit CI Status Release Notes
8 3.0 4 ci-swift3 Pending
8 2.3 3 ci-master 2015/10
7 2.2 3 ci-master 2015/10
6 1.2 2 2015/05
* N/A 1† ci-legacy

† PromiseKit 1 is pure Objective-C and thus can be used with any Xcode, it is also your only choice if you need to support iOS 7 or below.


We maintain some branches to aid migrating between Swift versions:

Xcode Swift PromiseKit Branch CI Status
8.0 2.3 2 swift-2.3-minimal-changes ci-23
7.3 2.2 2 swift-2.2-minimal-changes ci-22
7.2 2.2 2 swift-2.2-minimal-changes ci-22
7.1 2.1 2 swift-2.0-minimal-changes ci-20
7.0 2.0 2 swift-2.0-minimal-changes ci-20

We do not backport fixes (mostly) to these migration-branches, but pull-requests are welcome.

Support

About

Promises for Swift & ObjC

http://promisekit.org

License:MIT License


Languages

Language:Swift 56.5%Language:Objective-C 42.0%Language:Ruby 1.5%