Promisekit: Initializing Promise and fulfilling later gives: PromiseKit: warning: pending promise deallocated

Created on 7 Oct 2019  路  5Comments  路  Source: mxcl/PromiseKit

Promise 6.2.4 installed via CocoaPods
I'm trying to use PromiseKit with ReSwift thunks and running into an issue which I feel might be down to my understanding of initializing Promises.

func foo() -> (Thunk, Promise) {
   let promise = Promise()
   let thunk = Thunk { ... promise.fulfill() ... }
   return (thunk, promise)
}
let (thunk, promise) = foo()
dispatch(thunk)
promise.done { ... }

My question is how do I initialize a Promise that returns [String :Any] instead of the Void of Promise() ?

Most helpful comment

Your code uses systems and abstractions I have not seen before, so unless you can simplify it down to a minimal case I cannot help.

All 5 comments

let (pendingPromise, seal) = Promise<[String: Any]>.pending()

For more info see our documentation.

Thanks for the response @mxcl ! I updated it and looked the docs and still seem to be running into the same issue.

    public static func login(email: String, password: String) -> (Thunk<Any>, Promise<[String : Any]>) {
        let (loginPromise, seal) = Promise<[String: Any]>.pending()

        let loginThunk = Thunk<Any> { dispatch, getState in
            dispatch(ActionLogin())           
                UserConfig.userSessionManager.login() .responseJSON(completionHandler: { (dataResponse) in
                        switch dataResponse.result {
                        case .success(let value):
                            guard let json = value as? [String : Any] else {
                                seal.reject(UserError.malformedJson)
                                return
                            seal.fulfill(json)
                        case .failure(let error):
                            seal.reject(convertNetworkResponseError(error: error))
                        }
                    })

            loginPromise.done { json -> Void in
            //Success Case


                }.catch { error in
            // Failure Case
            }
        }
        return (loginThunk,loginPromise)
    }

The network call is made once I dispatch the thunk and then fulfilled based off of the returned results, however I still keep getting the same PromiseKit: warning: pending promise deallocated. At what point would it be getting deallocated?

Your code uses systems and abstractions I have not seen before, so unless you can simplify it down to a minimal case I cannot help.

Sorry about that! So this is the problematic code I believe:

 public static func login(email: String, password: String) -> (Thunk<Any>, Promise<[String : Any]>) {
        let (loginPromise, seal) = Promise<[String: Any]>.pending()

        let loginThunk = Thunk<Any> { dispatch, getState in          
            //Issue network call and fulfill the promise. seal.fullfill(json)

           loginPromise.done { json -> Void in
                //set auth information

                }.catch { error in

                 //handle auth error
            }
        }
        return (loginThunk,loginPromise)
    }

On my frontend, I am triggering this by dispatching the action and then performing some navigation logic:

        reduxWorker.getStore().dispatch(action)

        //Created helper methods to allow for navigation testing
            result.done { json -> Void in
                self.loginSuccessful()
            }.catch { error in
                self.loginFailure()
            }

However, each time I trigger it, it gives me me that error. As you can see the function is called and then after, I retrieve the result of the successful promise. My question is, would you happen to have an idea as why the promise is deallocated?

Per our documentation, this usually happens when there is a path in your code where the promise is neither fulfilled or rejected.

I recommend reading our docs.

Was this page helpful?
0 / 5 - 0 ratings