Last active
October 13, 2015 12:04
-
-
Save redent/a4658be406c4e7ae1e42 to your computer and use it in GitHub Desktop.
FutureKit extensions with some convenience methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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