0

我用 Promise<T> 返回类型包装了我的 2 个 API 请求:

func registerWithEmail(email: String, firstName: String, lastName: String, password: String, subscribe: Bool) -> Promise<Bool>

func requestAccessTokenWithEmail(username: String, password: String) -> Promise<String>

单独使用它们可以正常工作:

firstly {
    client.registerWithEmail("foo@gmail.com", firstName: "john", lastName: "smith", password: "password", subscribe: false)
    }.then { success -> Void in
        completion(success: success)
    }.catch { error -> Void in
        println(error.localizedDescription)
}

和:

firstly {
    client.requestAccessTokenWithEmail("foo@gmail.com", password: "password")
    }.then { accessToken -> Void in
        println(accessToken)
        completion(success: true)
    }.catch { error -> Void in
        println(error.localizedDescription)
}

then但是,当我链接它们时,永远不会填充第二个调用的令牌参数:

firstly {
    client.registerWithEmail(username, firstName: "a", lastName: "b", password: password, subscribe: false).then { success -> Void in
        return client.requestAccessTokenWithEmail(username, password: password)
    }.then { accessToken -> Void in
        println(accessToken)
        completion(success: true)
    }
}

如果我打破这个闭包,我无法查看accessToken参数,只能查看外部completion闭包。requestAccessTokenWithEmail但是,如果我在函数内部中断,accessToken则在调用fulfill.

我对 PromiseKit 很陌生,所以如果我在做一些愚蠢的事情,请告诉我。

我正在使用 PromiseKit 2.0、Swift 1.2、Xcode 6.4

4

1 回答 1

1

问题是第一个then闭包的签名。

它是success -> Void但必须是success -> Promise<String>

我认为这会被 Swift 的类型推断所捕获,但我忘记了它then被重载以接受(T) -> Promise<U>and的闭包(T) -> U

希望当 Swift 修复then闭包中显式签名的需要时,它会自动推断出来。

于 2015-07-21T19:54:12.630 回答