Skip to content

Best way to decide which promises to call #1133

Closed

Description

@Sergozh

Hello! Could you provide any idea of how to decide which promises should I use according to local data?

For example: I have to register user this is promise No.1, also I have promise No.2 for sending his Push notifications token to server. The problem is how to call fulfilling promises No.1 & No.2 if I have enough data for promise No.2 and how to call just No.1 when I haven't data for promise No.2?
Should I use nested closures such as:

promise1()
.done {
  promise2()
  .done {
  }
}

Big Thanks!

Activity

  1. mxcl commented on Mar 5, 2020

    @mxcl
    Owner

    Your question is not clear enough for me I’m afraid.

  2. Sergozh commented on Mar 5, 2020

    @Sergozh
    Author

    I'll try to describe:
    I have number of API calls which are already return Promise and I try to understand which is the right way to use promises in situation when: I receive some data from first API call and then I should decide to call next promise or not according to previous response

    Some code here:

    func registerUser(with activeWindow: UIWindow?) {
            let userService = UserService()
            
            if let receipt = AppstoreService.receiptData {
                let request = ReceiptRequest(
                    receipt: receipt.base64EncodedString(options: .lineLength64Characters))
                
                when(fulfilled: userService.register(RegisterRequest.current), userService.validateReceipt(request))
                .done { [weak self] (response, _) in
                    guard let self = self else { return }
                    
                    SettingsManager.shared.serverToken = response.token
                    NotificationsManager.shared.sendUserId()
                    
                    self.openApp()
                }.catch { [weak self] error in
                    guard let self = self else {
                        return
                    }
                    
                    self.showError(error, on: activeWindow)
                }
            } else {
                userService.register(RegisterRequest.current)
                    .done { [weak self] response in
                        guard let self = self else { return }
                        
                        SettingsManager.shared.serverToken = response.token
                        NotificationsManager.shared.sendUserId()
                        
                        self.openApp()
                }.catch { [weak self] error in
                    guard let self = self else {
                        return
                    }
                    
                    self.showError(error, on: activeWindow)
                }
            }
        }
    
  3. CristianCardosoA commented on May 20, 2020

    @CristianCardosoA

    This is not a promise question, is an architecture question.

  4. mxcl commented on May 23, 2020

    @mxcl
    Owner

    It is not clear to me still, one does not “call” a promise.

    Try reading our documentation.

  5. Repository owner locked and limited conversation to collaborators on May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions