The difference between ensure and finally is not clear in the docs. As far as my understanding, ensure can be called before catch and finally can be called after catch. But both functions are executed after the whole promise chain.
Is this correct?
Finally is executed after the whole chain. Ensure is executed where it is in the chain.
Like both are executed WHERE THEY ARE in the chain. Not sure how you came to think otherwise.
Hey @mxcl
In the commit that references this issue you write:
finallyis the same asensure, but it is not chainable
However, there's another subtle difference between the two--ensure can be called on a DispatchQueue of a user's choice whereas finally can't. Am I correct in assuming that a user should resort to using ensure as a substitute for finally if they're interested in calling the closure on a specific queue? Also, I'm curious to know why finally has the limit of not being able to be called on any queue?
Thanks!
This is an omission, finally should take a queue. PR welcome.
@mxcl Done: https://github.com/mxcl/PromiseKit/pull/876
Reading the V6 news item, I find the difference a bit confusing too.
Looking at the example in the PromiseKit readme:
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)
}
vs the use of finally:
firstly {
when(fulfilled: fetchImage, fetchLocation)
}.done { image, location in
self.imageView.image = image
self.label.text = "\(location)"
}.catch { error in
self.show(UIAlertController(for: error), sender: self)
}.finally {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
Would the difference be that with the first example, the isNetworkActivityIndicatorVisible = false code is executed before the self.show block, and with the second example it is executed after the self.show line?
Is that the only difference, or does it have any other side effects?
Would the difference be that with the first example, the isNetworkActivityIndicatorVisible = false code is executed before the self.show block, and with the second example it is executed after the self.show line?
That's correct.
Is that the only difference, or does it have any other side effects?
This is the only difference with the example code.
Most helpful comment
That's correct.
This is the only difference with the example code.