KO
|
EN
gitlite — search
Search
#python
#java
#python3
#arduino
#golang
#machine-learning
#rust
#html
#flask
#javascript
#seismology
#nodejs
swift-async
★ 16
Open GitHub ↗
Group async tasks together, swiftly
Download README (.md)
Explore Similar Repositories
Hermes
:
A fully featured messaging package for Laravel.
Raspberry-Pi-SPI-slave
:
Software SPI slave for the Raspberry Pi
gradle-getdown-plugin
:
A gradle plugin to bundle java app + jre with getdown support
ostara
:
No description available.
dhcping
:
dhcp test auditing suite
// repository documentation
Was this content helpful?
★ 0
(0 ratings)
Select Rating:
★
★
★
★
★
Submit Feedback
Recent Feedback
×
Download README
Do you want to download the
README.md
file for
swift-async
?
Download (.md)
# swift-async `swift-async` provides a common interface for asynchronous control flow. It allows You to group async tasks, like loading data from the network or performing long running computations, together with a unified completion handler. Inpired by [node-async](https://github.com/caolan/async). ## Usage For the sake of simplicity, consider the following model: ```swift class Room { let number: Int } class Floor { let name: String } class Building { func performAsyncWork1(completionHandler: (NSError?) -> ()) { // perform async work and invoke completionHandler when done } func fetchRoomsOnFloor(floor: Floor, completionHandler: ([Room]?, NSError?) -> ()) { // fetch some data here and invoke completionHandler when done } func fetchFloors(completionHandler: ([Floor]?, NSError?) -> ()) { // fetch some data here and invoke completionHandler when done } } ``` ### Executing tasks in parallel ```swift let building1 = Building() let building2 = Building() let tasks = [ Async.bind { building1.fetchFloors($0) }, Async.bind { building1.fetchRoomsOnFloor(Floor(name: "First floor"), $0) }, Async.bind { building2.fetchFloors($0) }, Async.bind { building2.fetchRoomsOnFloor(Floor(name: "First floor"), $0) }, ] Async.parallel(tasks) { (results, error) in if let error = error { println("Error: \(error)") } else if let results = results { let floors = results[0] as [Floor] let rooms = results[1] as [Room] println("Got floors: \(floors)") println("And rooms: \(rooms)") } } ``` ### Executing tasks one after another ```swift let building1 = Building() let building2 = Building() let tasks = [ building1.performAsyncWork1, building2.performAsyncWork2, Async.bind { building1.performAsyncWorkWithObject(nil, completionHandler: $0) }, ] Async.series(tasks) { (error) in if let error = error { return println("Error: \(error)") } else { println("all tasks succeeded") } } ``` ## Asumptions All functions must take a unified completion handler that either takes * a single `NSError?` argument * or a generic type `T?` and a `NSError?` argument `swift-async` is __not__ thread safe, so You need to make sure that all completion handlers are executed on the same thread. --- ## Contact - [Oliver Letterer](http://github.com/OliverLetterer) ([@oletterer](https://twitter.com/oletterer)) ## License swift-async is released under an MIT license. See LICENSE for more information.