TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jun 2015 |
SPMSupports SPM | ✗ |
Maintained by nghialv.
Swift µframework providing Future<T, Error>.
This library is inspired by the talk of Javier Soto at SwiftSubmit2015 and the Future
implementation in Scala.
And this is using antitypical/Result
.
func requestRepository(repoId: Int64, completion: (Repository?, NSError?) -> Void) {}
func requestUser(userId: Int64, completion: (User?, NSError?) -> Void) {}
// get owner info of a given repository
requestRepository(12345) { repo, error in
if let repo = repo {
requestUser(repo.ownerId) { user, error in
if let user = user {
// do something
} else {
// error handling
}
}
} else {
// error handling
}
}
let future = requestRepository(12345)
.map { $0.ownerId }
.flatMap(requestUser)
future.onCompleted { result in
switch result {
case .Success(let user): println(user)
case .Failure(let error): println(error)
}
}
Shorthand by using operator
let future = requestRepository(12345) <^> { $0.ownerId } >>- requestUser
future.onCompleted { result in
switch result {
case .Success(let user): println(user)
case .Failure(let error): println(error)
}
}
map
<^>
flatMap
>>-
filter
andThen
recover
zip
flatten
Using Carthage
- Insert
github "nghialv/Future"
to your Cartfile- Run
carthage update
Using Cocoapods
- Insert
use_frameworks!
to your Podfile- Insert
pod "Future"
to your Podfile- Run
pod install
Using Submodule