Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vadymmarkov committed May 27, 2017
1 parent d03148d commit 21f8e88
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ state and then could be resolved with a value or rejected with an error.
```swift
// Creates a new promise that could be resolved with a String value
let promise = Promise<String>()

// Resolves the promise
promise.resolve("String")

// Or rejects the promise
promise.reject(Error.notFound)
```
Expand All @@ -80,7 +78,7 @@ let promise = Promise({
```

```swift
// Creates a new promise that is rejected with an ErrorType
// Creates a new promise that is rejected with an Error
let promise = Promise({
//...
throw Error.notFound
Expand All @@ -96,48 +94,62 @@ let promise = Promise<String>(queue: dispatch_get_main_queue())
```

### Done
Add a handler to be called when the ***promise*** object is resolved with a value:
Adds a handler to be called when the ***promise*** object is resolved with a value:

```swift
// Create a new promise in a pending state
let promise = Promise<String>()

// Add done callback
promise.done({ value in
print(value)
})

// Resolve the promise
promise.resolve("String")
```

### Fail
Add a handler to be called when the ***promise*** object is rejected with
an `ErrorType`:
Adds a handler to be called when the ***promise*** object is rejected with
an `Error`:

```swift
// Create a new promise in a pending state
let promise = Promise<String>()

// Add done callback
// Add fail callback
promise.fail({ error in
print(error)
})

// Reject the promise
promise.reject(Error.notFound)
```

It's also possible to cancel a promise, which means it will be rejected with
`PromiseError.cancelled` error. `FailurePolicy` can be used if you want to
ignore this error in your `fail` handler:

```swift
// Create a new promise in a pending state
let promise = Promise<String>()
// This callback will not be called when a promise is cancelled
promise.fail({ error in
print(error)
})
// This callback will be called when a promise is cancelled
promise.fail(policy: .allErrors, { error in
print(error)
})
// Cancel the promise
promise.cancel()
```

### Always
Add a handler to be called when the ***promise*** object is either resolved or
Adds a handler to be called when the ***promise*** object is either resolved or
rejected. This callback will be called after [done](#done) or [fail](#fail)
handlers.

```swift
// Create a new promise in a pending state
let promise = Promise<String>()

// Add done callback
// Add always callback
promise.always({ result in
switch result {
case let .success(value):
Expand All @@ -146,7 +158,6 @@ promise.always({ result in
print(error)
}
})

// Resolve or reject the promise
promise.resolve("String") // promise.reject(Error.notFound)
```
Expand Down

0 comments on commit 21f8e88

Please sign in to comment.