Skip to content

Instantly share code, notes, and snippets.

@malcommac
Created February 4, 2017 11:14
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 malcommac/2023f0f4d76f83b4e1fe7bc015788d4b to your computer and use it in GitHub Desktop.
Save malcommac/2023f0f4d76f83b4e1fe7bc015788d4b to your computer and use it in GitHub Desktop.
Hydra_Internal_7.swift
@discardableResult
public func `catch`(in context: Context? = nil, _ body: @escaping ((Error) throws -> (Void))) -> Promise<Void> {
let ctx = context ?? .main
let nextPromise = Promise<Void>(in: ctx) { resolve, reject in
let onResolve = Observer<Value>.onResolve(ctx, { _ in
resolve(())
})
let onReject = Observer<Value>.onReject(ctx, { error in
do {
try body(error)
} catch let error {
reject(error)
}
resolve(())
})
self.add(observers: onResolve,onReject)
}
nextPromise.runBody()
self.runBody()
return nextPromise
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment