I am finding extremely hard to use PromiseKit 6.13.1 in an apparently simple situation.
I have the following two functions returning a Promise<String>
but I cannot seem to find a way to use them with ```firstly{}.then{} syntax:
func promiseGetJWTToken() -> Promise<String> {
return Promise<String> { seal in
let error: Error = NSError(domain: "", code: 2000)
getJWTToken { tokenJWT in
guard let tokenJWT = tokenJWT else {
seal.resolve(.rejected(error))
return
}
seal.resolve(.fulfilled(tokenJWT))
}
}
}
func promiseGetBEToken() -> Promise<String> {
return Promise<String> { seal in
let error: Error = NSError(domain: "", code: 2000)
getBEToken { result in
switch result {
case .success(let response):
guard let tokenBE = response.token else {
seal.resolve(.rejected(error))
return
}
seal.fulfill(tokenBE)
case .failure(let error):
debugPrint(error)
seal.resolve(.rejected(error))
case .none:
seal.resolve(.rejected(error))
}
}
}
}
When I try to use the following as follows
firstly {
promiseGetJWTToken()
}.then { tokenJWT in
// no need go on because I have issues already here
}
I receive:
Type '()' cannot conform to 'Thenable'; only struct/enum/class types can conform to protocols
I have also tried, which comes from autocompletion:
promiseGetJWTToken().then { token -> Thenable in
// no need go on because I have issues already here
}
In this case I receive:
Protocol 'Thenable' can only be used as a generic constraint because it has Self or associated type requirements
I decided to give PromiseKit a try because I have three network calls dependent on each other on cascade, but I wouldn't expect this to be so hard. Can anyone show me what am I doing wrong?
The error message is misleading; the real issue is that the .then closure should return a new Thenable. In your examples, the .then closures are empty.
Or just use .done, if not chaining promises.
They replaced that usage of then { }
with done { }
.
firstly {
promiseGetJWTToken()
}.done { tokenJWT in
// use your token
}
You can try
firstly {
promiseGetJWTToken()
}.then { tokenJWT -> Promise<String> in
return promiseGetBEToken()
}
or
firstly {
promiseGetJWTToken()
}.then { tokenJWT -> Promise<String> in
promiseGetBEToken()
return Promise.value(tokenJWT)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With