Ian Warburton
Ian Warburton

Reputation: 15706

Why must I return a promise?

Here is the code...

login().then {

    // our login method wrapped an async task in a promise
    return API.fetchKittens()

}.then { fetchedKittens in

    // our API class wraps our API and returns promises
    // fetchKittens returned a promise that resolves with an array of kittens
    self.kittens = fetchedKittens
    self.tableView.reloadData()

}.catch { error in

    // any errors in any of the above promises land here
    UIAlertView(…).show()

}

See how the then method is not returning anything.

When I use then the compiler is saying I must return a promise. Why don't I have the choice not to?

The error goes away when I add a catch clause straight after. huh?

Upvotes: 4

Views: 2039

Answers (2)

Filip Miladinovic
Filip Miladinovic

Reputation: 101

You should use .done{} to finish promise chain, like this:

.done { fetchedKittens -> Void in }

.then{} with -> Void is not working anymore, because .then{} always need to return promise.

Documentation

Upvotes: 3

Ian Warburton
Ian Warburton

Reputation: 15706

Answer here.

I added -> Void after my closure parameter.

.then { fetchedKittens -> Void in }

Upvotes: 3

Related Questions