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