Home:ALL Converter>Why do JavaScript promises chain rejections into resolved promises?

Why do JavaScript promises chain rejections into resolved promises?

Ask Time:2017-06-03T04:04:54         Author:Reactgular

Json Formatter

I'm wondering why rejections are chained as resolved with ES6 promises?

Here's an example:

let p = Promise.reject().then(function () {
    return 'one';
}, function() {
    return 'two';
});

p.then(function (value) {
   // I do not expect this to be executed, since it was not resolved.
   console.log(value);
});

The above outputs "two" to the console.

https://jsfiddle.net/pscb88xg/1/

Why does the chaining of a promise mutate a rejection into a successful resolve?

Edit: I want to clarify that the question has practical application.

What if you want to convert data from A to B using chaining.

 p.then(function (A) {
     return new B(A);
 });

The above mutates rejections into resolved values. Even if no reject callback is used.

For example;

 let p = Promise.reject('error').then(function (A) {
      return new B(A);
 });

 // some code elsewhere

 p.then(function (B) {
      console.log('success!');
 });

In the above example. The value B is not B but the error, and it was resolved successfully later in the chain.

Is this normal?

Edit: I understand my confusion now. I was extracting HTTP header values in rejections like this.

 let p = myHttpService.get(...).then(function() {
          //....
 }, function(response) {
          // get headers
 });

The above was chaining my promises to a resolved value, and I didn't understand why. I can fix my code with the following.

 let p = myHttpService.get(...).then(function() {
          //....
 }, function(response) {
          // get headers
          return Promise.reject(response);
 });

Author:Reactgular,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44336640/why-do-javascript-promises-chain-rejections-into-resolved-promises
Paul :

After handling an error you usually want your code to continue, similar to how code after a catch block runs like normal, whereas uncaught exceptions abort.\n\nIf you want to abort instead then don't handle the error until the end of the chain:\n\nlet p = Promise.reject().then(function () {\n return 'one';\n});\n\np.then(function (value) {\n // This won't run, because the rejection hasn't been handled yet\n console.log(value);\n}, function() {\n return console.log( 'there was a problem' );\n}).then(function ( ) {\n // This will run because the rejection has been dealt with already.\n console.log( 'Moving on');\n});\n",
2017-06-02T20:10:02
Michał Perłakowski :

MDN documentation for Promise.prototype.then says:\n\n\n After the invocation of the handler function [the function passed to then()], the promise returned by then gets resolved with the returned value as its value.\n",
2017-06-02T20:10:51
toskv :

It's meant to allow you to gracefully recover from an error in a promise chain.\n\nAn example might be the 304 Not Modified response from the server. If you were to use a promise based library to do an http request any response that's not 2XX will be considered a failure and the promise will be rejected. From an application's point of view however 304 might just as good as a 200 and you'd like to continue as normal.",
2017-06-02T20:08:40
yy