Promisekit: Get first response object in PromiseKit then done block

Created on 3 Mar 2020  路  5Comments  路  Source: mxcl/PromiseKit

I'm calling three services in a row. When I'm calling third service, I need to use variable from first service response which is userModel. I can get second service response which is initModel but can't reach first model userModel. My Question is that how can I use userModel in done block by returning it then blocks?

P.S: I tried to return -> Promise<(UserModel,InstallationModel)> in first call but because UserModel is already an object not a promise, I need to convert it to promise to return it. This looks like me a bad way to do it.

As you can see I'm storing it with self.userModel = userModelwhich I don't wanna do.

func callService(completionHandler: @escaping (Result<UserModel>) -> Void) {
    SandboxService.createsandboxUser().then { userModel -> Promise<InstallationModel> in
        self.userModel = userModel
        return SandboxService.initializeClient(publicKey: self.keyPairs.publicKey)
    }.then { initModel -> Promise<DeviceServerResponseModel> in
        self.initModel = initModel
        if let unwrappedUserModel = self.userModel {
            return SandboxService.deviceServerServiceCaller(authKey: initModel.token.token,apiKey:unwrappedUserModel.apiKey,privaKey: self.keyPairs.privateKey)
        }
        throw ServiceError.handleParseError()
    }.then { serverResponseModel -> Promise<UserModel> in
        if let unwrappedInitModel = self.initModel, let unwrappedUserModel = self.userModel {
            return SandboxService.sessionServiceCaller(authKey: unwrappedInitModel.token.token, apiKey: unwrappedUserModel.apiKey, privaKey: self.keyPairs.privateKey)
        }
        throw ServiceError.handleParseError()
    }.done { userModel in
        completionHandler(Result.success(userModel))
    }.catch { error in
        completionHandler(Result.error(error))
    }
}

Most helpful comment

func callService(completionHandler: @escaping (Result<UserModel>) -> Void) {
    SandboxService.createsandboxUser().then { userModel in
        firstly {
            SandboxService.initializeClient(publicKey: self.keyPairs.publicKey)
        }.then { initModel in
            SandboxService.deviceServerServiceCaller(authKey: initModel.token.token, apiKey: userModel.apiKey,privaKey: self.keyPairs.privateKey).map{ ($0, initiModel) }
        }.then { serverResponseModel, initModel in
            SandboxService.sessionServiceCaller(authKey: initModel.token.token, apiKey: userModel.apiKey, privaKey: self.keyPairs.privateKey)
        }
    }.pipe(to: completionHandler)
}

All 5 comments

func callService(completionHandler: @escaping (Result<UserModel>) -> Void) {
    SandboxService.createsandboxUser().then { userModel in
        firstly {
            SandboxService.initializeClient(publicKey: self.keyPairs.publicKey)
        }.then { initModel in
            SandboxService.deviceServerServiceCaller(authKey: initModel.token.token, apiKey: userModel.apiKey,privaKey: self.keyPairs.privateKey).map{ ($0, initiModel) }
        }.then { serverResponseModel, initModel in
            SandboxService.sessionServiceCaller(authKey: initModel.token.token, apiKey: userModel.apiKey, privaKey: self.keyPairs.privateKey)
        }
    }.pipe(to: completionHandler)
}

Can you please show me how can I call a function eg; self.delegate?.myDelFunction just after userModel in what it should be return? Also how can I handle error cases in this block?

Can you please show me how can I call a function

鈥ust call it? I don鈥檛 really understand the question.

Also how can I handle error cases in this block?

Add a catch, but it doesn鈥檛 seem like you want to do that, doesn鈥檛 the completionHandler handle the error?

When I write;

func callService(completionHandler: @escaping (Result<UserModel>) -> Void) {
    SandboxService.createsandboxUser().then { userModel in
        //What I want to call
        delegate?.updateSomething()
        firstly {
            delegate?.updateSomething()
            SandboxService.initializeClient(publicKey: self.keyPairs.publicKey)
        }.then { initModel in
            delegate?.updateSomething()
            SandboxService.deviceServerServiceCaller(authKey: initModel.token.token, apiKey: userModel.apiKey,privaKey: self.keyPairs.privateKey).map{ ($0, initiModel) }
        }.then { serverResponseModel, initModel in
            delegate?.updateSomething()
            SandboxService.sessionServiceCaller(authKey: initModel.token.token, apiKey: userModel.apiKey, privaKey: self.keyPairs.privateKey)
        }
    }.pipe(to: completionHandler)
}

I needed to write what I should return in userModel -> MyReturnObject in but I don't know what should I write instead of MyReturnObject.

For second question;

I want to change completion handler return type to void and call delegate functions instead. Therefore, I need to change it to Done and Catch blocks.

Was this page helpful?
0 / 5 - 0 ratings