jquery promise return when ajax call error

François Richard

I'm using backbone to fetch a model + jquery promise to be sure it's fetched.

I call this function

rippleidcollection.createIdList(param).then // do some stuff

This works well when fetch triggered success but when there is error triggered the then part of the above function is not executed (and it's a problem cause I want to trigger an error message then).

Here is the code of createdIdList function:

createIdList: function(toresolves) {
        var self = this;

        var xhrs = _.map(toresolves, function(toresolve,i) {
                var id = toresolve.id;
                var model = new RippleId({id:id}, address);
                var xhr = model.fetch({
                    success: function(model,response) {
                        self.add(model);
                    },
                    error: function(model, response) {
                        self.add(model);
                    }
                });       
            return xhr;     
        });

        var sync = $.when.apply(null, xhrs);
        return sync;
    }

So when there is a success (of model.fetch) rippleidcollection.createIdList(param).then // do some stuff is executed passing the payload but not when error is triggered. All I want is that the error payload is passed too so I can manage error message ...e'etc

I think I'm missing something basic about promises but i'm not sure what. thanks for your help!

Benjamin Gruenbaum

then takes a second parameter for handling errors:

rippleidcollection.createIdList(param).then(function(result){
   // stuff to do on success
}, function(err){
   // deal with error case, your code for handling errors goes here.
});

Please consider changing your backend to take a list of IDs and add them at batch instead - it would be a lot more efficient and make a lot less requests.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related