0

I created a promise which gets some data from API and returns it. Now what i am stuck is after getting the json data i need to store them on device storage (realm). After calling the promise when i try with then i get some error stating Cannot convert value of type '(Any) -> Void' to expected argument type '(_) -> _' . But when i try with done it works perfectly. But i need to use then to save the data on realm.

Following approch works fine

Translator().translators().done { data -> Void in
            print(data[0].name)
            }.catch { error in
                print(error)
        }

But following two approach fails

Translator().translators().then() { data -> Void in
            print(data)
            }.catch() {
                (e: Error) in
                print(e)
        }

Above gives an error that says

Cannot convert value of type '(Any) -> Void' to expected argument type '(_) -> _'

and following approach gives another similar error

Translator().translators().then() {
            (data: [Translator]) -> Void in
            print(data[0].name)
            }.catch() {
                (e: Error) in
                print(e)
        }

error message:

Cannot convert value of type '([Translator]) -> Void' to expected argument type '([Translator]) -> _'

Promise i created:

func translators() -> Promise<[Translator]> {
        return firstly {
            Alamofire.request("http://192.168.0.107:3000/translatdors", method: .get).responseData()
            }.map {
                try JSONDecoder().decode([Translator].self, from: $0.data)
        }
    }

Image of my code and error enter image description here

Ahsan Aasim
  • 1,177
  • 3
  • 14
  • 40

1 Answers1

2

You have to return a promise when you do .then because until .done you are not finished. I don't see any issue in saving realm data in .done as your method has no return type.

Incase you still want to use .then, it can be done this way.

Translator().translators().then({ data -> Promise<[Translator]> in
            return Promise.value(data)
            }).done({ data in
            // Save to realm
           }).catch({ error in
              print(error.localizedDescription)
           })
Kamran
  • 14,987
  • 4
  • 33
  • 51
  • I am actually playing with promisekit. Now I understand the issue. got misdirected by the error message. thanks brother. – Ahsan Aasim Apr 24 '18 at 12:06