google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.79k stars 292 forks source link

Documentation question #137

Closed jeff-h closed 4 years ago

jeff-h commented 4 years ago

In the docs for then() it states:

The then operator ... expects another promise, a value, or an error to be returned.

This compiles as expected:

let promise = Promise<Void>.pending()
.then {
    return "a"
}

This doesn't compile, with the error Unexpected non-void return value in void function.

let promise = Promise<Void>.pending()
.then {
    print("a")
    return "a"
}

I suspect the single-line examples leverage Swift 5.1's 'implicit returns' to infer the return type.

Hopefully I'm not exposing some embarrassing personal ignorance of Swift here but can anyone explain what I need to do to get the second example to compile?

jeff-h commented 4 years ago

If it helps anyone else, I found that explicitly specifying the return type resolves the issue:

        let myPromise = Promise<Void>.pending()
        .then { _ -> String in
            print("a")
            return "a"
        }

...in which case the myPromise variable is of type Promise<String>.

Would something like this be useful to put into the docs? Happy to create a PR.