❗️Archived now❗️
Since Apple released Combine
framework, I decide to archive this repo.
You still can use this repo as an example of Future
/Promise
implementation in Swift
.
EasyFutures
Swift implementation of Futures & Promises. You can read more about Futures & Promises in Wikipedia: https://en.wikipedia.org/wiki/Futures_and_promises.
EasyFutures is:
- 100% Swift, 100% test coverage.
- easy to understand.
- type safe (uses Swift generics).
- the avoidance of a "callback hell".
- out of the box errors handling (you don't need to use
do/catch
). - composeable (
map
,flatMap
,filter
,recover
,zip
,andThen
,flatten
). - support sequences (
fold
,traverse
,sequence
). - fully documented.
Documentation
- Full documentation and more examples you can find in Playground (to use playground you should open it in
EasyFutures.xcodeproj
and build EasyFutures framework). - Wiki (full documentation and all examples).
- Unit tests.
- Examples section.
Requirements
- iOS 9.0+
Installation
CocoaPods:
- Add the following line to your
Podfile
:
- Add
use_frameworks!
to yourPodfile
. - Run
pod install
. - Add to files:
Examples
Traditional way to write asynchronous code:
Same logic but with EasyFutures:
Future
Future is an object that contains or will contain result
which can be value or error. Usually result gets from some asynchronous process. To receive result you can define onComplete
, onSuccess
, onError
callbacks.
Promise
The Promises are used to write functions that returns the Futures. The Promise contains the Future instance and can complete it.
Composition
map
Returns the new Future with the result you return to closure or with error if the first Future contains error.
flatMap
Returns the new Future with the Future you return to closure or with error if the first Future contains error.
filter
Returns the Future if value satisfies the filtering else returns error.
recover
If the Future contains or will contain error you can recover it with the new value.
zip
Combines two values into a tuple.
andThen
Returns the new Future with the same value.
flatten
If the value of the Future is the another Future you can flatten it.
Errors handling
map
, flatMap
, filter
and recover
can catch errors and return the Future with this error, so you don't need to handle it with do/catch
.
Sequences
EasyFutures provides some functions to help you work with the sequences of Futures.
fold
You can convert a list of the values into a single value. Fold returns the Future with this value. Fold takes default value and then you perform action with default value and every value from the list. Can catch errors.
traverse
Traverse can work with any sequence. Takes closure where you transform the value into the Future. Returns the Future which contains array of the values from the Futures returned by the closure.
sequence
Transforms a list of the Futures into the single Future with an array of values.
License
EasyFutures is under MIT license. See the LICENSE file for more info.