Hello,
In documentation and examples this coding style is used a lot:
firstly {
when(fulfilled: fetchImage, fetchLocation)
}.done { image, location in
self.imageView.image = image
self.label.text = "\(location)"
}.ensure {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}.catch { error in
self.show(UIAlertController(for: error), sender: self)
}
But it is not so usual for swift, any advantages for this?
In other libraries:
RxSwift
let searchResults = searchBar.rx.text.orEmpty
.throttle(0.3, scheduler: MainScheduler.instance)
.distinctUntilChanged()
.flatMapLatest { query -> Observable<[Repository]> in
if query.isEmpty {
return .just([])
}
return searchGitHub(query)
.catchErrorJustReturn([])
}
.observeOn(MainScheduler.instance)
signal
.mapError { (error: NSError) -> CustomError in
switch error.domain {
case "com.example.foo":
return .foo
case "com.example.bar":
return .bar
default:
return .other
}
}
.observeFailed { error in
print(error.rawValue)
}
let value = numbers
.map {$0 * 2}
.filter {$0 > 50}
.map {$0 + 10}
Our choice emphasizes that you read between the lines to see the sequence of operations, which I think is more readable for our use.
Promises are sequences of tasks, so this is different to the other examples you provide, which functionally modify things. With promises, mostly the function that you are calling is irrelevant, all that matters is the content of the closures.
Closing due to stagnation, if you have further questions reopen.
Most helpful comment
Our choice emphasizes that you read between the lines to see the sequence of operations, which I think is more readable for our use.
Promises are sequences of tasks, so this is different to the other examples you provide, which functionally modify things. With promises, mostly the function that you are calling is irrelevant, all that matters is the content of the closures.