[BREAKGLASS] Append-only mirror of github.com/signalapp/PromiseKit
Go to file
2018-02-15 16:09:23 -05:00
.github Update issue template 2017-08-09 11:40:35 -04:00
Documentation Update GettingStarted.md 2018-02-13 21:18:33 -05:00
Extensions Update Alamofire extension 2018-02-15 16:09:23 -05:00
PromiseKit.playground 5.0.0 2017-11-06 09:27:11 -05:00
PromiseKit.xcodeproj Provide wrap but deprecated 2018-02-13 16:24:35 -05:00
Sources Mark always as deprecated; Fixes #775 2018-02-15 16:09:23 -05:00
Tests Provide PMKRace(); Closes #696 2018-02-13 15:50:52 -05:00
.gitignore Ignore .DS_Store too 2016-09-27 08:43:32 -07:00
.gitmodules PromiseKit 4 / Swift 3 / Xcode 8 2016-09-07 15:45:21 -07:00
.travis.yml Travis deprecated Xcode 8.1/8.2; Bump to 9.2 2017-12-10 13:18:29 -05:00
docker-compose.yml Include docker-compose for building on linux. 2017-05-19 14:25:39 +02:00
LICENSE Update Copyright in LICENSE 2017-11-30 11:23:23 -05:00
Package.swift Fix SwiftPM 2018-02-13 16:44:01 -05:00
PromiseKit.podspec Support macOS 10.9 2018-02-13 16:24:35 -05:00
README.md [ci skip] Docs++ WIP 2018-02-13 16:44:01 -05:00
README.zh_CN.md Update NSURL in docs (#656) 2017-05-17 15:35:01 -04:00
README.zh_Hant.md Update NSURL in docs (#656) 2017-05-17 15:35:01 -04:00

PromiseKit

badge-pod badge-languages badge-pms badge-platforms Build Status

繁體中文 (outdated), 简体中文 (outdated)


Promises simplify asynchronous programming, freeing you up to focus on the more important things. They are easy to learn, easy to master and result in clearer, more readable code. Your co-workers will thank you.

UIApplication.shared.isNetworkActivityIndicatorVisible = true

let fetchImage = URLSession.shared.dataTask(.promise, with: url).flatMap{ UIImage(data: $0.data) }
let fetchLocation = CLLocationManager.requestLocation()

firstly {
    when(fulfilled: fetchImage, fetchLocation)
}.done { image, location in
    self.imageView.image = image
    self.label.text = "\(location)"
}.ensure {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
}.catch { error in
    self.show(UIAlertController(for: error), sender: self)
}

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. It is a top-100 pod used in many of the most popular apps in the world.

PromiseKit 6 Released

PromiseKit 6 has been released; read the release notes and migration guide.

Quick Start

In your Podfile:

use_frameworks!

target "Change Me!" do
  pod "PromiseKit", "~> 6.0"
end

PromiseKit 6, 5 and 4 support Xcode 8.1, 8.2, 8.3, 9.0, 9.1 and 9.2; Swift 3.0, 3.1, 3.2, 4.0 and 4.1 ; iOS, macOS, tvOS, watchOS, Linux and Android; CocoaPods, Carthage and SwiftPM; (CI Matrix).

For Carthage, SwiftPM, etc., or for instructions when using older Swifts or Xcodes see our Installation Guide.

Documentation

If you are looking for a functions documentation, then please note our sources are thoroughly documented.

Extensions

Promises are only as useful as the asynchronous tasks they represent, thus we have converted (almost) all of Apples APIs to promises. The default CocoaPod provides Promises and the extensions for Foundation and UIKit. The other extensions are available by specifying additional subspecs in your Podfile, eg:

pod "PromiseKit/MapKit"          # MKDirections().calculate().then { /*…*/ }
pod "PromiseKit/CoreLocation"    # CLLocationManager.requestLocation().then { /*…*/ }

All our extensions are separate repositories at the PromiseKit organization.

I don't want the extensions!

Then dont have them:

pod "PromiseKit/CorePromise", "~> 6.0"

Note

Carthage installations come with no extensions by default.

Choose Your Networking Library

Promise chains are commonly started with networking, thus we offer multiple options: Alamofire, OMGHTTPURLRQ and of course (vanilla) NSURLSession:

// pod 'PromiseKit/Alamofire'
// https://github.com/PromiseKit/Alamofire
firstly {
    Alamofire.request("http://example.com", method: .post, parameters: params).responseJSON()
}.done { rsp in
    // `rsp.json`
}.catch { error in
    //…
}

// pod 'PromiseKit/OMGHTTPURLRQ'
// https://github.com/PromiseKit/OMGHTTPURLRQ
firstly {
    URLSession.POST("http://example.com", JSON: params)
}.flatMap {
    try JSONDecoder().decoder(Foo.self, with: $0.data)
}.done { foo in
    //…
}.catch { error in
    //…
}

// pod 'PromiseKit/Foundation'
// https://github.com/PromiseKit/Foundation
firstly {
    URLSession.shared.dataTask(.promise, with: try makeRequest())
}.flatMap {
    try JSONDecoder().decode(Foo.self, with: $0.data)
}.done { foo in
    //…
}.catch { error in
    //…
}

func makeRequest() throws -> URLRequest {
    var rq = URLRequest(url: url)
    rq.httpMethod = "POST"
    rq.addValue("application/json", forHTTPHeaderField: "Content-Type")
    rq.addValue("application/json", forHTTPHeaderField: "Accept")
    rq.httpBody = try JSONSerialization.jsonData(with: obj)
    return rq
}

Nowadays, considering that:

  • We almost always POST JSON
  • We now have JSONDecoder
  • PromiseKit now has flatMap

We recommend vanilla URLSession; use less black-boxes, stick closer to the metal. Alamofire was essential until the three bulletpoints above became true, but nowadays it isnt really necessary. OMGHTTPURLRQ was developed before JSON was the modern standard and thus REST requests were hard, but nowadays you rarely network anything but JSON.

Support

Ask your question at our Gitter chat channel or on our bug tracker.