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.
# 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.
We have thorough and complete documentation at promisekit.org.
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)
}
}
Complete and progressive learning guide at promisekit.org.
PromiseKit contains Swift, so we engage in an unending battle with Xcode:
Xcode | Swift | PromiseKit | CI Status | Release Notes |
---|---|---|---|---|
8 | 3.0 | 4 | Pending | |
8 | 2.3 | 3 | 2015/10 | |
7 | 2.2 | 3 | 2015/10 | |
6 | 1.2 | 2 | – | 2015/05 |
* | N/A | 1† |
† 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 |
---|---|---|---|---|
7.3 | 2.2 | 2 | swift-2.2-minimal-changes | |
7.2 | 2.2 | 2 | swift-2.2-minimal-changes | |
7.1 | 2.1 | 2 | swift-2.0-minimal-changes | |
7.0 | 2.0 | 2 | swift-2.0-minimal-changes |
We do not backport fixes (mostly) to these migration-branches, but pull-requests are welcome.
- Ask questions of the developers and the community at our Gitter chat channel.
- Ask your question by opening a ticket.