google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation question

jeff-h opened this issue · comments

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?

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.