Skip to content

Instantly share code, notes, and snippets.

@redent
Last active October 13, 2015 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redent/a4658be406c4e7ae1e42 to your computer and use it in GitHub Desktop.
Save redent/a4658be406c4e7ae1e42 to your computer and use it in GitHub Desktop.
FutureKit extensions with some convenience methods.
import Foundation
import FutureKit
extension Future {
// Executes the block iff the future is successful and leaves the result immutable
public final func then(executor : Executor, block: T throws -> Void) -> Future<T> {
return onSuccess(executor) { (result: T) -> Completion<T> in
do {
try block(result)
return .Success(result)
}
catch let error {
return .Fail(error)
}
}
}
public final func then(block: T throws -> Void) -> Future<T> {
return then(.Primary, block: block)
}
}
import Foundation
import FutureKit
extension Future {
public final func onSuccessTry<__Type>(executor: Executor, block:(result:T) throws -> __Type) -> Future<__Type> {
return onSuccess(executor) { result -> Completion<__Type> in
do {
let blockResult = try block(result: result)
return .Success(blockResult)
}
catch let error {
return .Fail(error)
}
}
}
public final func onSuccessTry<__Type>(block:(result:T) throws -> __Type) -> Future<__Type> {
return onSuccess { result -> Completion<__Type> in
do {
let blockResult = try block(result: result)
return .Success(blockResult)
}
catch let error {
return .Fail(error)
}
}
}
}
import Foundation
import FutureKit
extension Future {
/// Converts the current future to a Future<Void>, ignoring any result.
/// Useful for combining different Futures if you don't care about the results
func toVoid() -> Future<Void> {
return onSuccess { result -> Void in
return ()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment