PromiseKit icon indicating copy to clipboard operation
PromiseKit copied to clipboard

Add `@discardableResult` annotation to `then`

Open zlangley opened this issue 10 years ago • 11 comments

  • 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
}

zlangley avatar Sep 21 '16 23:09 zlangley

Dupes #548. This is intentional.

mxcl avatar Sep 22 '16 00:09 mxcl

Thanks for the quick reply. Makes sense.

zlangley avatar Sep 22 '16 00:09 zlangley

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 avatar Sep 22 '16 00:09 mxcl

@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.

donpark avatar Oct 21 '16 18:10 donpark

It seems reasonable to require the user to explicitly mark that the situation does not require a catch though.

mxcl avatar Oct 21 '16 18:10 mxcl

if it seems reasonable to you, go with it. I only complied with your request for examples.

donpark avatar Oct 21 '16 18:10 donpark

I appreciate the example very much, thanks.

mxcl avatar Oct 21 '16 18:10 mxcl

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.

jomnius avatar Oct 27 '16 06:10 jomnius

Yes, _ = is recommended. Or figure out why you are not catching, or provide an example here.

mxcl avatar Oct 27 '16 15:10 mxcl

Yeah, _ = is what I use too.

I think the most compelling example of catch-less Promise is infallible async operations.

donpark avatar Oct 27 '16 16:10 donpark

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("…")
}

zlangley avatar Oct 27 '16 16:10 zlangley