0

Is PromiseKit 6.0 structure Correct or not?? because i am receiving an error when i running my app :(

Here's the image:

enter image description here

 //Promise block
        firstly{
            removePreviusSearch(text)
            }.then {(finished) -> Promise<AI> in
                aiService.getAi()
            }.done {(ai) -> Void in
                self.updateResults(ai)
            }.catch { (error) in
                //catch error
    }

}

// MARK : Remove previus search
func removePreviusSearch(_ newText: String) -> Promise<Bool> {
    return Promise { seal in
        UIView.animate(withDuration: 0.5, animations:{
            self.topLabel.alpha = 0
            self.mainText.alpha = 0
            self.resultsView.alpha = 0
            self.textField.text = ""
        }, completion: { (finished: Bool) in
            UIView.animate(withDuration: 0.5) {
                self.topLabel.alpha = 1
                self.mainText.alpha = 1
            }
            seal.reject(finished as! Error)
            self.topLabel.text = "user says".uppercased()
            self.mainText.text = newText
            self.setLabel(self.cityLabel)
            self.setLabel(self.streetLabel)
            self.setLabel(self.countryLabel)
            self.setLabel(self.dateLabel)
            self.setLabel(self.speechLabel)
            self.setLabel(self.itentLabel)
            self.setLabel(self.conditionLabel)
            self.setLabel(self.outfitLabel)
            self.setLabel(self.scoreLabel)
        })
    }
}
coder
  • 8,346
  • 16
  • 39
  • 53

1 Answers1

1
      completion: { (finished: Bool) in
        ...
        seal.reject(finished as! Error) // casting error
        ... 

First, finished is a boolean, you cannot force cast it as a Error. If you want to reject with an error, you should init an Error like below.

let error = NSError(domain: "some information", code: 0, userInfo: nil) as Error
reject(error)

Second, I don't know why you want to reject in the middle of the code, it will break the whole promise chain. You should fulfill the promise after all things are correctly done.

    }, completion: { (finished: Bool) in
        UIView.animate(withDuration: 0.5) {
            self.topLabel.alpha = 1
            self.mainText.alpha = 1
        }
        self.topLabel.text = "user says".uppercased()
        self.mainText.text = newText
        self.setLabel(self.cityLabel)
        self.setLabel(self.streetLabel)
        self.setLabel(self.countryLabel)
        self.setLabel(self.dateLabel)
        self.setLabel(self.speechLabel)
        self.setLabel(self.itentLabel)
        self.setLabel(self.conditionLabel)
        self.setLabel(self.outfitLabel)
        self.setLabel(self.scoreLabel)

        // fulfill your promise right here
    })
...