How rsvp.js handles rejected promises with a failure callback chain

Re: https://github.com/tildeio/rsvp.js

I have a function called doSomething () that does something for a while and then returns RSVP.Promise. Then a chain of successful and unsuccessful callbacks is registered by the returned promise (see code below). The behavior that I expected was if the promise was fulfilled, the chain of successful callbacks registered with the promise will be dismissed, and if the promise is rejected (unsuccessfully), the chain of callbacks with refusals will be activated.

I get the expected behavior when the promise is fulfilled, but I get compared to what I expected when the promise is rejected. That is, successful callbacks are linked to each other, and the output of one successful callback is passed to the next success callback in the chain. But it seems that the failure callbacks are not chained. They behave almost like a catch in a try / catch block (see code and output below).

Can someone explain this behavior? Is this the way it is supposed to work, or is it an error in the way rsvp.js handles the rejected / unsuccessful promise that has a failure chain of calls registered with it? Now I am reading the Promises / A + specification to try to figure it out, but if someone knows this from the head, I would like to hear your explanation. Thanks in advance.

jsfiddle: http://jsfiddle.net/rylie/VYSj7/2/

doSomething() // returns an RSVP.Promise object .then( function(message) { console.log("then success 1: " + message); return "from success 1"; }, // success callback function(message) { console.log("then failure 1: " + message); return "from failure 1"; } // failure callback ) .then( function(message) { console.log("then success 2: " + message); return "from success 2"; }, // success callback function(message) { console.log("then failure 2: " + message); return "from failure 2"; } // failure callback ) .then( function(message) { console.log("then success 3: " + message); return "from success 3"; } // success callback ) .then( null, function(message) { console.log("then failure 4: " + message); return "from failure 4"; } // failure callback ) .then( function(message) { console.log("then success 5: " + message); return "from success 5"; }, // success callback function(message) { console.log("then failure 5: " + message); return "from failure 5"; } // failure callback ); 

** When the promise is fulfilled (successfully), this is the result that I get and expect:

 then success 1: Promise fulfilled! then success 2: from success 1 then success 3: from success 2 then success 5: from success 3 

** When the promise is rejected (failure), this is the result that I get:

 then failure 1: Promise rejected! then success 2: from failure 1 then success 3: from success 2 then success 5: from success 3 

** This is what I expected (on rejection / bad promise):

 then failure 1: Promise rejected! then failure 2: from failure 1 then failure 4: from failure 2 then failure 5: from failure 4 
+6
source share
1 answer

You should just forget that .then() even takes more than one argument and uses the .catch method, and that will make more sense.

Promises provide compliance with some synchronization code constructs, but this is not very noticeable when you only have a low .then() level. What you are looking for is basically an array of callback / callback aggregation, but that is not the point of promises at all.

Think of .catch(fn) as well as .then(null, fn) :

 doSomething().then(function(val) { console.log(val); }).catch(function(e) { console.error(e); }); 

Parallels synchronization code (submit doSomething returns synchronously):

 try { var val = doSomething(); console.log(val); } catch(e) { console.error(e); } 

A few catches (remember that .catch is the more readable alias .then(null, fn)

 doSomething().then(function(val) { console.log(val); }).catch(function(e) { return e; }).catch(function(e){ //Will not get here ever //because error thrown from doSomething() //was handled in the upper catch which doesn't trow }); 

Parallels:

 try { try { var val = doSomething(); console.log(val); } catch(e) { //no need for explicit return e } } catch( e ) { //Will not get here ever //because error thrown from doSomething() //was handled in the upper catch which doesn't trow } 

So, now you should notice that you can create the expected result, instead of returning http://jsfiddle.net/VYSj7/3/

.catch() is an IMO - an important method for providing a library of promises, which will also be included in the built-in Javascript promises in the future. However, if such a method is not provided, you can (or there must be, unfortunately, implementations that do not use prototypes):

 Promise.prototype.catch = function(fn) { return this.then(null, fn); }; 

See also deviation turns into execution

+11
source

Source: https://habr.com/ru/post/957079/


All Articles