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
firstly {
when(URLSession.dataTask(with: url).asImage(), CLLocationManager.promise())
}.then { image, location -> Void in
self.imageView.image = image
self.label.text = "\(location)"
}.always {
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.
In your Podfile:
use_frameworks!
swift_version = "3.0"
pod "PromiseKit", "~> 4.0"For Carthage, SwiftPM, etc., or for instructions when using older Swifts or Xcodes see our Installation Guide.
- Handbook
- Manual
- Installation Guide
- Objective-C Guide
- Troubleshooting (eg. solutions to common compile errors)
- Appendix
If you are looking for a function’s documentation, then please note our sources are thoroughly documented.
Promises are only as useful as the asynchronous tasks they represent, thus we
have converted (almost) all of Apple’s APIs to promises. The default CocoaPod
comes with promises for UIKit and Foundation, the rest can be installed by
specifying additional subspecs in your Podfile, eg:
pod "PromiseKit/MapKit" # MKDirections().promise().then { /*…*/ }
pod "PromiseKit/CoreLocation" # CLLocationManager.promise().then { /*…*/ }All our extensions are separate repositories at the PromiseKit organization.
Promise chains are commonly started with networking, thus we offer multiple
options: Alamofire, OMGHTTPURLRQ and of course (vanilla) NSURLSession:
// pod 'PromiseKit/Alamofire'
Alamofire.request("http://example.com", withMethod: .POST).responseJSON().then { json in
//…
}.catch { error in
//…
}
// pod 'PromiseKit/OMGHTTPURLRQ'
URLSession.POST("http://example.com").asDictionary().then { json in
//…
}.catch { error in
//…
}OMGHTTPURLRQ supplements NSURLSession (so you can, eg. do POSTs properly).
However, nowadays, servers often simply prefer JSON so you can opt to use
NSURLSession by itself and if so
we wrote the promises you need.
Ask your question at our Gitter chat channel or on our bug tracker.
