0

I have wrapped 2 of my API requests with Promise<T> return types:

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

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

Using them individually works fine:

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

And:

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

However, when I chain them, the token argument to the second then call is never populated:

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

If I break inside this closure I can't view the accessToken argument, only the outer completion closure. However, if I break inside the requestAccessTokenWithEmail function, the accessToken argument is populated when calling fulfill.

I'm quite new to PromiseKit, so please let me know if I'm doing something stupid.

I'm using PromiseKit 2.0, Swift 1.2, Xcode 6.4

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Senior
  • 2,259
  • 1
  • 20
  • 31

1 Answers1

1

The problem was the signature of the first then closure.

It was success -> Void but needed to be success -> Promise<String>

I thought that would've been caught by Swift's type inference but I forgot that then is overloaded to accept closures of both (T) -> Promise<U> and (T) -> U.

Hopefully when Swift fixes the need for the explicit signature in the then closure it will be automatically inferred.

Senior
  • 2,259
  • 1
  • 20
  • 31