From d03c35f4e1f5995bff4bcddf1f6c496f9879329b Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Sun, 28 May 2017 16:03:13 +0200 Subject: [PATCH] Update readme --- README.md | 21 +++++++++++++++++++++ Sources/When/Promise.swift | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/README.md b/README.md index e257266..a2e4a03 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ asynchronous code up to the next level. * [Fail](#fail) * [Always](#always) * [Then](#then) + * [Recover](#recover) * [When](#when) * [Installation](#installation) * [Author](#author) @@ -229,6 +230,26 @@ promise2.thenInBackground({ data -> Promise in }) ``` +### Recover +Returns a new ***promise*** that can be used to continue the chain when an +error was thrown. + +```swift +let promise = Promise() +// Recover the chain +promise + .recover({ error -> Promise in + return Promise({ + return "Recovered" + }) + }) + .done({ string in + print(string) // Recovered + }) +// Reject the promise +promise.reject(Error.notFound) +``` + ### When Provides a way to execute callback functions based on one or more ***promises***. The ***when*** method returns a new "master" ***promise*** that diff --git a/Sources/When/Promise.swift b/Sources/When/Promise.swift index 1d7e12f..8c5171c 100644 --- a/Sources/When/Promise.swift +++ b/Sources/When/Promise.swift @@ -222,6 +222,9 @@ extension Promise { // MARK: - Recover extension Promise { + /** + Helps to recover from certain errors. Continues the chain if a given closure does not throw. + */ public func recover(on queue: DispatchQueue = mainQueue, _ body: @escaping (Error) throws -> T) -> Promise { let promise = Promise(queue: queue) @@ -229,6 +232,9 @@ extension Promise { return promise } + /** + Helps to recover from certain errors. Continues the chain if a given closure does not throw. + */ public func recover(on queue: DispatchQueue = mainQueue, _ body: @escaping (Error) throws -> Promise) -> Promise { let promise = Promise(queue: queue) @@ -243,6 +249,9 @@ extension Promise { return promise } + /** + Adds a recover observer. + */ private func addRecoverObserver(on queue: DispatchQueue, promise: Promise, _ body: @escaping (Error) throws -> T?) { observer = Observer(queue: queue) { result in