diff --git a/README.md b/README.md index 7b24322..e257266 100644 --- a/README.md +++ b/README.md @@ -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() - // Resolves the promise promise.resolve("String") - // Or rejects the promise promise.reject(Error.notFound) ``` @@ -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 @@ -96,48 +94,62 @@ let promise = Promise(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() - // 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() - -// 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() +// 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() - -// Add done callback +// Add always callback promise.always({ result in switch result { case let .success(value): @@ -146,7 +158,6 @@ promise.always({ result in print(error) } }) - // Resolve or reject the promise promise.resolve("String") // promise.reject(Error.notFound) ```