https://github.com/fpg1503/asynchronous
Implementation-agnostic asynchronous code
https://github.com/fpg1503/asynchronous
agnostic-implementation async-programming asynchronous cocoapods documented futures promise-library promises swift swift-4 tested
Last synced: 10 months ago
JSON representation
Implementation-agnostic asynchronous code
- Host: GitHub
- URL: https://github.com/fpg1503/asynchronous
- Owner: fpg1503
- License: mit
- Created: 2017-11-05T02:47:29.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-28T23:16:43.000Z (over 7 years ago)
- Last Synced: 2025-02-02T17:08:28.369Z (12 months ago)
- Topics: agnostic-implementation, async-programming, asynchronous, cocoapods, documented, futures, promise-library, promises, swift, swift-4, tested
- Language: Swift
- Homepage:
- Size: 836 KB
- Stars: 14
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Asynchronous
[](https://travis-ci.org/fpg1503/Asynchronous)
[](http://cocoapods.org/pods/Asynchronous)
[](http://cocoapods.org/pods/Asynchronous)
[](http://cocoapods.org/pods/Asynchronous)
[](http://cocoadocs.org/docsets/Asynchronous/)
[](https://codecov.io/gh/fpg1503/Asynchronous)
Asynchronous is a one-stop shop for your async needs, the user can use the subspecs to automatically run the Async code using completion handlers, [BrightFutures](https://github.com/Thomvis/BrightFutures), [HydraAsync](https://github.com/malcommac/Hydra), [PromiseKit](https://github.com/mxcl/PromiseKit), [Promises](https://github.com/khanlou/Promise), [then](https://github.com/freshOS/then) and much more!
The ultimate answer to **which asynchronous abstraction should I use in my API?**, just use `Asynchronous` and give your users freedom without the headache of adding several dependencies and different abstractions to your code!
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Sample use case
Let's say you're writing a mobile SDK that does asynchronous tasks (such as HTTP requests), just like the following:
```swift
func getUser(by id: String) -> Async {
return Async { resolve, reject in
let url = APIRouter.route(for: .users, id: id)
let request = URLRequest(url: url)
let session = URLSession.shared
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
reject(error) // You can also `throw error`!
} else if let data = data {
do {
let user = try JSONDecoder().decode(User.self, from: data)
resolve(user)
} catch (let error) {
reject(error)
}
} else {
reject(NSError(domain: "my.domain", code: 123))
}
}
task.resume()
}
}
```
That's it! You simply create an `Async` and call `resolve` or `reject` once your task is finished.
The user can either add a completion block or use their favorite asynchronous abstraction:
### Completion
```swift
apiClient.getUser(by: id).async { user, error in
if let user = value {
userName.text = user.name
} else {
display(error: error)
}
}
```
### Promise
```swift
apiClient.getUser(by: id).promise()
.then { user in
userName.text = user.name
}.catch { error in
display(error: error)
}
```
## Installation
Async is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'Asynchronous'
```
## Available Subspecs
| Name | Description |
|-------|------------|
| `Asynchronous/Alamofire` | [Alamofire](https://github.com/Alamofire/Alamofire) support |
| `Asynchronous/BrightFutures` | [BrightFutures](https://github.com/Thomvis/BrightFutures) support |
| `Asynchronous/HydraAsync` | [HydraAsync](https://github.com/malcommac/Hydra) support |
| `Asynchronous/PromiseKit` | [PromiseKit](https://github.com/mxcl/PromiseKit) support |
| `Asynchronous/Promises` | [Promises](https://github.com/khanlou/Promise) support |
| `Asynchronous/Then` | [then](https://github.com/freshOS/then) support |
## Contributing
Contributions are encourajed and appreciated!
For more information take a look at [CONTRIBUTING.md](CONTRIBUTING.md).
### Roadmap
- [ ] ReactiveSwift support
- [x] RxSwift support
- [x] Tests
- [x] Better documentation
- [ ] Improved README
- [x] Remove BrightFutures dependency
- [ ] Carthage support
- [ ] SwiftPM support
- [x] Fix unnecessary error type erasures
- [ ] Fix tuple gate in Asyncify
- [ ] Improve Example project
## Author
Francesco Perrotti-Garcia, fpg1503@gmail.com
## License
Asynchronous is available under the MIT license. See the LICENSE file for more info.