Skip to content

Instantly share code, notes, and snippets.

@jarsen
Created July 13, 2015 16:59
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 jarsen/45186a114abfa44220e8 to your computer and use it in GitHub Desktop.
Save jarsen/45186a114abfa44220e8 to your computer and use it in GitHub Desktop.
attemptMap for Reactive Cocoa that deals with `throws`
func attemptMap<T, U>(operation: T throws -> U) -> ReactiveCocoa.Signal<T, NSError> -> ReactiveCocoa.Signal<U, NSError> {
return { signal in
return Signal { observer in
signal.observe(next: { value in
do {
sendNext(observer, try operation(value))
}
catch {
sendError(observer, error as NSError)
}
}, error: { error in
sendError(observer, error)
}, completed: {
sendCompleted(observer)
}, interrupted: {
sendInterrupted(observer)
})
}
}
}
@jarsen
Copy link
Author

jarsen commented Jul 13, 2015

The only thing about this is because ErrorType is so loosey goosey we have to bridge to NSError, and we lose type information 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment