Add `@discardableResult` annotation to `then`
- PromiseKit version: 4.0
- Xcode version: 8.0
I noticed that catch and tap have the @discardableResult annotation, but then does not. It seems perfectly reasonable to ignore the result of then in many cases.
Adding this annotation allows us to do away with the ugly underscore assignment.
// Without an assignment, Swift gives a warning.
_ = PHPhotoLibrary.requestAuthorization().then { authorized in
print(authorized) // => true or false
}
// By annotating `then` with `@discardableResult`, Swift allows us to ignore the result without a warning.
PHPhotoLibrary.requestAuthorization().then { authorized in
print(authorized) // => true or false
}
Dupes #548. This is intentional.
Thanks for the quick reply. Makes sense.
I'm willing to be wrong on this, discussion welcome. IME there aren't many times where the chain shouldn't be returned or terminated so the trade off feels right.
@mxcl there are often times when some async. task needs to be performed inline inside a .then closure which returns the result via outer resolve because task is either too incidental to turn into a Promise returning function or to wrap with nested Promise needlessly.
Example:
func foo() -> Promise<URL> {
return Promise<URL> { resolve, reject in
firstly {
// do something async
}.then { something in
DispatchQueue.main.async {
// blah blah
resolve(url)
}
}
}
}
In this example, error is either not expected or intentionally ignored. When error matters, reject would be called. Either case, trailing catch is omitted. Sloppy practice, maybe, but everyday code nonetheless.
It seems reasonable to require the user to explicitly mark that the situation does not require a catch though.
if it seems reasonable to you, go with it. I only complied with your request for examples.
I appreciate the example very much, thanks.
I have the same problem and noticed that this ticket is closed. However I couldn't clearly see the fix: just keep using the ugly _ = syntax? Not really complaining, just curious of what is the recommended fix.
Yes, _ = is recommended. Or figure out why you are not catching, or provide an example here.
Yeah, _ = is what I use too.
I think the most compelling example of catch-less Promise is infallible async operations.
I think the most compelling example of catch-less Promise is infallible async operations.
This is the example I was going to give, but they seem rare in enough in practice. Also, it may still be better to catch anyway and assert your claim:
infallibleTask().then {
// …
}.catch { _ in
assertionFailure("…")
}