Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vadymmarkov committed May 28, 2017
1 parent bfb3ea1 commit d03c35f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -229,6 +230,26 @@ promise2.thenInBackground({ data -> Promise<NSData> in
})
```

### Recover
Returns a new ***promise*** that can be used to continue the chain when an
error was thrown.

```swift
let promise = Promise<String>()
// Recover the chain
promise
.recover({ error -> Promise<String> 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
Expand Down
9 changes: 9 additions & 0 deletions Sources/When/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,19 @@ 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<T> {
let promise = Promise<T>(queue: queue)
addRecoverObserver(on: queue, promise: promise, body)
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<T>) -> Promise<T> {
let promise = Promise<T>(queue: queue)
Expand All @@ -243,6 +249,9 @@ extension Promise {
return promise
}

/**
Adds a recover observer.
*/
private func addRecoverObserver(on queue: DispatchQueue, promise: Promise<T>,
_ body: @escaping (Error) throws -> T?) {
observer = Observer(queue: queue) { result in
Expand Down

0 comments on commit d03c35f

Please sign in to comment.