Future 0.0.3

Future 0.0.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2015
SPMSupports SPM

Maintained by nghialv.



Future 0.0.3

Future

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.

Why we need Future?

Traditional async code
 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
    }
 }
Code with Future
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)
    }
}

Usage

  • map <^>
  • flatMap >>-
  • filter
  • andThen
  • recover
  • zip
  • flatten

Installation

  • 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

Requirements

  • Swift 1.2 (Xcode 6.3 or later)
  • iOS 8.0 or later