Lightweight library for waiting for completion of several concurrent tasks.
Library works with asynchronous operations wrapped to Job
struct. To wrap your asynchronous task just write:
func findPerson(byId id: String, completionHandler: @escaping (Result<Person, PersonError>) -> Void) {
// some asynchronous work
}
func findPersonJob(id: String) -> Job<Person> {
return Job { onSuccess, onFailure in
findPerson(byId: id) { result in
switch result {
case let .success(person):
onSuccess(person)
case let .failure(error):
onFailure(error)
}
}
}
}
Then you can use when
function to wait for completion of several jobs:
when(findPersonJob(id: "personA"), findPersonJob(id: "personB"), onSuccess: { personA, personB in
// do something with `personA` and `personB`
}, onError: { error in
// handle error
})
For cases when your asynchronous method signature is similar to findPerson(byId:completionHandler:)
function above(list of parameters at the beginning, then completionHandler that takes Result
type) When
has special function wrap
which allows easily wrap such methods to jobs:
when(wrap(findPerson(byId:completionHandler:))("personA"),
wrap(findPerson(byId:completionHandler:))("personB"),
onSuccess: { personA, personB in
// do something with `personA` and `personB`
},
onError: { error in
// handle error
})
Also you can easily implement your own wrap
functions. Just see Wrap.swift for example. Pull requests with new wrap
functions are welcome!
When is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "JustWhen"
SiarheiFedartsou, siarhei.fedartsou@gmail.com
When is available under the MIT license. See the LICENSE file for more info.