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());
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With