Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PromiseKit 6.13.1 cannot conform to 'Thenable' when I try to use Promise<String> functions

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?

like image 880
Fabrizio Prosperi Avatar asked Jun 18 '20 10:06

Fabrizio Prosperi


3 Answers

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.

like image 92
pipacs Avatar answered Nov 09 '22 01:11

pipacs


They replaced that usage of then { } with done { }.

firstly {
   promiseGetJWTToken()
 }.done { tokenJWT in
   // use your token
 }
like image 7
user2759839 Avatar answered Nov 09 '22 02:11

user2759839


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)
 }
like image 2
v2panda Avatar answered Nov 09 '22 01:11

v2panda