Description
When is a lightweight implementation of Promises
in Swift. It doesn't include any helper functions for iOS and OSX and it's
intentional, to remove redundant complexity and give you more freedom and
flexibility in your choices. It is type safe, thanks Swift generics, so you
could create promises with whatever type you want.
When can easily be integrated into your projects and libraries to move your
asynchronous code up to the next level.
Why?
To make asynchronous code more readable and standardized:
Usage
Promise
A promise represents the future value of a task. Promises start in a pending
state and then could be resolved with a value or rejected with an error.
Callbacks of the current promise and all the chained promises
(created with then) are executed on the main queue by default, but
you can always specify the needed queue in init
:
Done
Adds a handler to be called when the promise object is resolved with a value:
Fail
Adds a handler to be called when the promise object is rejected with
an Error
:
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:
Always
Adds a handler to be called when the promise object is either resolved or
rejected. This callback will be called after done or fail
handlers.
Then
Returns a new promise that can use the result value of the current
promise. It means that you could easily create chains of promises to
simplify complex asynchronous operations into clear and simple to understand
logic.
A new promise is resolved with the value returned from the provided
closure:
A new promise is resolved when the promise returned from the
provided closure resolves:
then closure is executed on the main queue by default, but you can pass a
needed queue as a parameter:
If you want to use background queue there are the helper methods for this case:
Recover
Returns a new promise that can be used to continue the chain when an
error was thrown.
When
Provides a way to execute callback functions based on one or more
promises. The when method returns a new "master" promise that
tracks the aggregate state of all the passed promises. The method will
resolve its "master" promise as soon as all the promises resolve,
or reject the "master" promise as soon as one of the promises is
rejected. If the "master" promise is resolved, the done callback is
executed with resolved values for each of the promises:
Reactive extensions
Use the following extension in order to integrate When with RxSwift:
Installation
When is available through CocoaPods. To install
it, simply add the following line to your Podfile:
For RxSwift
extensions you can use CocoaPods subspecs:
When is also available through Carthage.
To install just write into your Cartfile:
Author
Vadym Markov, markov.vadym@gmail.com
Credits
Credits for inspiration go to PromiseKit
and Then.
Contributing
Check the CONTRIBUTING file for more info.
License
When is available under the MIT license. See the LICENSE file for more info.