PromiseKit README

The project's README from github.com/mxcl/PromiseKit. As one markdown file: README.md.

PromiseKit

badge-pod badge-languages badge-pms badge-platforms codecov


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).compactMap{ UIImage(data: $0.data) }
let fetchLocation = CLLocationManager.requestLocation().lastValue

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 that has 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.

codecov

Quick Start

In your Podfile:

use_frameworks!

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

PromiseKit 8 supports recent Xcodes (13+). Some Podspecs were dropped as a result. Pull requests are welcome.

PromiseKit 6, 5 and 4 support Xcode 8.3, 9.x and 10.0; Swift 3.1, 3.2, 3.3, 3.4, 4.0, 4.1, 4.2, 4.3 and 5.0 (development snapshots); iOS, macOS, tvOS, watchOS, Linux and Android; CocoaPods, Carthage and SwiftPM; (CI Matrix).

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

PromiseKit and Swift 5.5+ Async/Await

As of Swift 5.5, the Swift language now offers support for built-in concurrency with async / await. See Async+ for a port of PromiseKit's most useful patterns to this new paradigm.

Professionally Supported PromiseKit is Now Available

TideLift gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools.

Get Professional Support for PromiseKit with TideLift.

Documentation

Extensions

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 provides Promises and the extensions for Foundation and UIKit. The other extensions are available by specifying additional subspecs in your Podfile, e.g.:

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 don’t have them:

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

Note: Carthage installations come with no extensions by default.

Networking

Promise chains commonly start with a network operation. Thus, we offer extensions for URLSession:

// pod 'PromiseKit/Foundation'  # https://github.com/PromiseKit/Foundation

firstly {
    URLSession.shared.dataTask(.promise, with: try makeUrlRequest()).validate()
    // ^^ we provide `.validate()` so that eg. 404s get converted to errors
}.map {
    try JSONDecoder().decode(Foo.self, with: $0.data)
}.done { foo in
    //…
}.catch { error in
    //…
}

func makeUrlRequest() 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 JSONEncoder().encode(obj)
    return rq
}

Support for Alamofire is welcome, please submit a PR.

Support

Please check our Troubleshooting Guide, and if after that you still have a question, ask at our Gitter chat channel or on our bug tracker.

Security & Vulnerability Reporting or Disclosure

https://tidelift.com/security

PromiseKit (mxcl/PromiseKit)

Searching Database…

这是什么东西?PromiseKit 是一套面向 Swift 和 Objective-C 的完整 Promise 实现,用来简化异步编程,让你的异步代码更清晰、更易读,也更容易掌握。
它是加到我应用里的库,还是单独运行的工具?它是一个库:你通过包管理器把它加到 Swift 或 Objective-C 项目里,它在你的应用内部运行。任何有 Swift 编译器的平台都能用它。
怎么把它加到我的项目里?用 CocoaPods 加到 Podfile 里,或者通过 Carthage、SwiftPM、Accio 安装——README 推荐使用 Carthage 或 Accio。PromiseKit 8 需要 Xcode 13 或更新版本。
它跟我的 Xcode 和 Swift 版本兼容吗?PromiseKit 8 支持较新的 Xcode(13 及以上),部分 podspec 也因此被移除了。旧版本线 6、5 和 4 则覆盖 Xcode 8.3 到 10.0,以及 Swift 3.1 到 5.0。
Swift 现在内置了 async/await,还有必要加 PromiseKit 吗?README 自己就指向了 Async+——它把 PromiseKit 最有用的模式移植到了新的 async/await 范式上,是 Swift 5.5+ 代码的现代替代方案。
它只能在苹果平台上用吗,还是只支持 iOS?它为 iOS、macOS、tvOS 和 watchOS 提供了精心设计的定制功能,但任何有 swiftc 的平台都能用,包括 Linux——旧版本 4 到 6 甚至列出过 Android。
这些扩展是做什么的?PromiseKit 已经把苹果几乎所有的 API 都转换成了返回 Promise 的形式。默认的 CocoaPod 包内置 Foundation 和 UIKit 的扩展;其他扩展是单独的 subspec(包括用于网络请求的 URLSession),而通过 Carthage 安装时默认不带任何扩展。
PromiseKit 在真实的生产应用里有在用吗?有——它是排名前 100 的 CocoaPod,被全球许多最热门的应用使用。
我能在 Objective-C 里用它吗?可以——PromiseKit 的 Objective-C 桥接非常出色,文档里还有专门的 Objective-C 指南。
团队可以买到专业的支持服务吗?可以——PromiseKit 通过 TideLift 提供,为开发团队提供来自最懂它的专家的专业级保障和维护。
Creator
mxcl
Size
10.4 MB
Stars
14.2K
Age
12y
Commits · 90 Days