Search code examples
javascriptnode.jsbluebird

Promise fulfilled inspite of rejection


I am using bluebird settle method to check results for promises regardless of any rejections. In the secondMethod I have rejected the promise still I get isFulfilled() true.

var Promise = require('bluebird');

Promise.settle([firstMethod, secondMethod]).then(function(results){
    console.log(results[0].isFulfilled()); 
    console.log(results[1].isFulfilled()); 
   // console.log(results[1].reason());
}).catch(function(error){
    console.log(error);
});

    var firstMethod = function() {
   var promise = new Promise(function(resolve, reject){
      setTimeout(function() {
         resolve({data: '123'});
      }, 2000);
   });
   return promise;
};


var secondMethod = function() {
   var promise = new Promise(function(resolve, reject){
      setTimeout(function() {
         reject((new Error('fail')));
      }, 2000);
   });
   return promise;
};

Solution

  • I debugged your code and the code within your functions isn't being called. You need to actually call the functions :)

    Promise.settle([firstMethod(), secondMethod()]).then(function (results) {
        console.log(results[0].isFulfilled()); // prints "true"
        console.log(results[1].isFulfilled()); // prints "false"
        console.log(results[1].reason()); // prints "fail"
    }).catch(function (error) {
        console.log(error);
    });