【问题标题】:PromiseKit 2.0: chaining promises doesn't pass argumentPromiseKit 2.0:链接承诺不传递参数
【发布时间】:2015-10-10 03:19:11
【问题描述】:

我已经用 Promise 返回类型包装了我的 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 函数内部中断,则在调用 fulfill 时会填充 accessToken 参数。

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

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

【问题讨论】:

    标签: ios swift promisekit


    【解决方案1】:

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

    它是success -&gt; Void,但必须是success -&gt; Promise&lt;String&gt;

    我认为这会被 Swift 的类型推断捕获,但我忘记了 then 被重载以接受 (T) -&gt; Promise&lt;U&gt;(T) -&gt; U 的闭包。

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

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 2016-01-06
      • 2016-02-08
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 2014-10-30
      • 2018-11-30
      • 2017-10-09
      相关资源
      最近更新 更多