为什么我必须 return 一个承诺?

Why must I return a promise?

Here是代码...

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()

}

看看 then 方法如何不 returning 任何东西。

当我使用 then 时,编译器说我必须 return 一个承诺。为什么我没有选择不去?

当我紧接着添加一个 catch 子句时,错误消失了。嗯?

回答here

我在闭包参数后添加了-> Void

.then { fetchedKittens -> Void in }

你应该使用 .done{} 来完成承诺链,像这样:

.done { fetchedKittens -> Void in }

.then{} with -> Void 不再有效,因为 .then{} 总是需要 return 承诺。

Documentation