测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2017年8月 |
SwiftSwift版本 | 3.0 |
SPM支持SPM | ✗ |
由Damouse维护。
这是一个受Python Twisted和Promise/A+强烈启发的Swift时的Deferred实现。它还受到了ReactiveCocoa在管理值转换和Pipeline方面的影响。
该项目是为Swift 1.3编写的,尚未针对Swift 3进行更新。我现在没有更新它的计划。
基本上,Deferred通过将处理器函数链接到一起、转换或序列化数据以及管理错误来编排处理器函数的调用。它的主要重点是使异步代码(尤其是网络代码)更容易处理,但不依赖于任何特定的网络库实现。
import Deferred
// Create a new deferred
var d = Deferred<Void>()
// Build the chain. Values are passed down and transformed based on parameter types
// The next deferreds in the chain do not require signatures, their type is inferred from the previous
// deferred's return type
d.chain { () -> Deferred<String> in
print(1)
return f
}.then { s in
print(s)
print(2)
}.then {
print(3) // I dont take any args, since the block above me didnt reutn a deferred
}.error { err in
print("Error: \(err)")
}
// Fire the callback
d.callback([])
这是一个用于处理异步操作库。以下是一个更具象的与AF的示例
Alamofire.request(.GET, url).json("title") { (post: String) -> () in
// Loaded a single post. Request the rest of the article
return Alamofire.request(.GET, url).json("article)
}.chain { (article: String) -> () in
// Loaded the article. Return it as an array of words
return article.split(' ')
}.then { words in
print("Have words!")
}.error { e in
print("Error occured: \(e)")
}
功能列表
AnyFunction
泛型接收者和工厂方法