Promisekit: How can I know which promises is failed in .catch?

Created on 11 May 2018  路  6Comments  路  Source: mxcl/PromiseKit

Hello! I have 2 promises, which have to execute one by one.
First I need to upload images on server, then I need to update user info (not in a row).
So I wrote this code:

        firstly {
            ProfileManager.uploadImages(photos)
        }
        .then { (photos)  in
            ProfileManager.updateProfile(name, bio: bio ?? "", birthdayDate: birthdayDate ?? "")
        }
        .done { (result) in
                self.view?.showAlert(title: nil, message: "袛邪薪薪褘械 褍褋锌械褕薪芯 懈蟹屑械薪械薪褘")
        }
        .catch { (error) in
                print() // how can I know uploadImages or  updateProfile was failed??
        }
        .finally {
                        self.view?.stopLoading()
                }

how can I know in the .catch section uploadImages or updateProfile was failed??

And if I want to do something with "photos", which uploadImages returned , I need to write .done section before .then, right?

As I understand I need to write (photos) in in .then section, but I can't do anything with this variable.

Most helpful comment

I meant, that it鈥檚 cool that in .when i can get each promises, but i need to do one by one
Nevermind) anyway than you so much)

All 6 comments

how can I know in the .catch section uploadImages or updateProfile was failed

Presumably the error passed into your catch will indicate which task failed. You鈥檒l have to check it.

For the photos, the simplest solution is to do something with them in your then where you currently have them or assign them to a higher level variable (e.g a property). If neither of these are good for you let me know. There are other options.

I can do nothing with photos in then, there is error then:
image

Swift can鈥檛 infer the return type of closures with 2+ lines. You鈥檒l have to specify the type of promise you鈥檙e returning from the closure.

Ok, thanks!

But I thought, that there is something like when(fullfiled) when I can get each promises .
Thought that in .done I can get all of previous completed results
And that .catch will return promise which failed.

Or that I can write something like that:

 firstly {
            ProfileManager.uploadImages(photos)
        }
        .catch { (error) in
                print() // error for photos
        }
        .then { (photos)  in
            ProfileManager.updateProfile(name, bio: bio ?? "", birthdayDate: birthdayDate ?? "")
        }
        .done { (result) in
                self.view?.showAlert(title: nil, message: "袛邪薪薪褘械 褍褋锌械褕薪芯 懈蟹屑械薪械薪褘")
        }
        .catch { (error) in
                print() // error for profile
        }
        .finally {
                        self.view?.stopLoading()
                }

So thanks for your help, I think all is fine now..

We do have when which behaves as you describe. Though I assumed that you wouldn鈥檛 want to update the profile until after the photos upload. If you want to do them at the same time, then when is what you want, and the following closure will contain the results of each of the promises passed in.

You can鈥檛 chain anything after a catch except for a finally. A catch will get called for any promise the errors prior in the chain. It鈥檚 up to you to determine which promise failed via the error object. This is a feature of promises. It reduces your error handling to a single place. Though there are alternatives when you really them.

done does not in fact contain the results of all prior promises in the chain as you describe. We don鈥檛 have a method for that. Though it sounds like when will fit the bill for you in this case.

I meant, that it鈥檚 cool that in .when i can get each promises, but i need to do one by one
Nevermind) anyway than you so much)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dboydor picture dboydor  路  4Comments

mxcl picture mxcl  路  4Comments

anilabsinc-ajay picture anilabsinc-ajay  路  6Comments

zvonicek picture zvonicek  路  3Comments

victoraraujo01 picture victoraraujo01  路  3Comments