Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuing after failing a promise

I have a case where a promise might fail, but I want to be able to handle that, and continue on to the next then. I've tried to return a successful promise from within the fail catch, but it gives an error about the return object not having the method set. Is this possible? How would I go about it?

Parse.Promise.as(1).then(function() {

    if (user.get('vendor')) {
        //fetch returns a promise
        return user.get('vendor').fetch();
    }
    return new Vendor();

}).fail(function() {
    //this will be called if the fetch fails, in that case, just return new Vendor();
    return Parse.Promise.as(function() {
        //this will be a valid promise so should hopefully return to the next then, but it doesn't work
        return new Vendor();
    });
}).then(function(result) {
    vendor = result;
    //continue with stuff
}).fail(function(error) {
    res.json(400, {
        "result": false,
        "error": error
    });
});

EDIT:

I tried changing it to:

Parse.Promise.as(1).then(function() {

    if (user.get('vendor')) {
        return user.get('vendor').fetch();
    }
    return new Vendor();

}).then(null, function() {
    //if the fetch fails, this will return a successful Promise with Vendor object
    console.log("failed fetch");
    return new Vendor();
}).then(function(result) {
    console.log("vendor retrieved");

}).then(null, function(error) {
    console.log('error');
});

But logged: failed fetch error

Is this just how Parse does it, or is something else wrong?

EDIT2:

Seems to work if I change the

return new Vendor();

line to

return Parse.Promise.as(1).then(function() { return new Vendor(); });

(edit) or this:

return Parse.Promise.as(new Vendor());
like image 891
phazei Avatar asked Apr 03 '14 04:04

phazei


People also ask

What happens when a Promise is rejected?

If the Promise rejects, the second function in your first . then() will get called with the rejected value, and whatever value it returns will become a new resolved Promise which passes into the first function of your second then. Catch is never called here either.

How do you handle a Promise rejection?

If an error condition arises inside a promise, you “reject” the promise by calling the reject() function with an error. To handle a promise rejection, you pass a callback to the catch() function. This is a simple example, so catching the rejection is trivial.

Can a Promise be rejected more than once?

It is not safe to resolve/reject promise multiple times. It is basically a bug, that is hard to catch, becasue it can be not always reproducible.

What happens if Promise all fails?

Promise.all fail-fast behaviorPromise.all is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, then Promise.all will reject immediately.


1 Answers

Just like you said, recovering from an exception is possible with promises:

try{
      mightThrow()
} catch (e){
     // handle
} 
thisWillRunRegardless();

Or with Parse promises:

Promise.as(1).then(function(){
    mightThrow();
}).then(null,function(e){
    // handle
}).then(function(){
    thisWillRunRegardless();
});

With other promise libraries it might look like:

Promise.try(function(){
     mightThrow();
}).catch(function(){
      //handle
]).then(thisWillRunRegardless);

The problem with your above code is the .fail. Since parse.com promises are jQuery complaint - their fail method acts like jQuery's. It adds a fail handler and returns the same promise.

Not sure why they did this, but oh well. You need to change .fail(function(){ to .then(null,function(){... instead. The second argument .then gets is the rejection handler.

like image 79
Benjamin Gruenbaum Avatar answered Sep 30 '22 11:09

Benjamin Gruenbaum