Why isn't this rejected RSVP promise not calling catch()?

1.6k views Asked by At

In my Ember RSVP code, I'm not able to get any error handlers called, even though I see that my 'reject' function is executing successfully. Code:

var promise = new Promise(function(resolve, reject) {

    doStuff().then(function() {

        resolve(1);

    }, function() {

        console.log('rejected!');
        reject('This has been rejected!');

    });

 });

var allPromises = [promise]; // Real code has more than one; just one for demonstration.

Ember.RSVP.all(allPromises)
.then(
    function() {
        console.log('I am called!');
    },
    function() { console.log('I am never called!');})
.catch(function(err) {

    console.log('I am never called, either.');

});

However, I do see the 'rejected!' message in the console. What have I done wrong here? Shouldn't the catch() fire since the reject() worked properly?

DEBUG: Ember : 1.9.0-beta.1

DEBUG: Ember Data : 1.0.0-beta.12

DEBUG: jQuery : 1.9.1

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

They state 'Sometimes you might want to work with many promises at once. If you pass an array of promises to the all() method it will return a new promise that will be fulfilled when all of the promises in the array have been fulfilled; or rejected immediately if any promise in the array is rejected.'

1

There are 1 answers

0
gabrielf On BEST ANSWER

You are catching the error in your onReject handler. If you want your catch to run you will have to either not supply a onReject handler or throw an error in the reject handler:

var promise = new Promise(function(resolve, reject) {
  reject("")
});

var allPromises = [promise];

// Alt 1 - Dont supply a onRejected handler
Ember.RSVP.all(allPromises)
    .then(function() {
        console.log('I am called!');
    }).catch(function(err) {
        console.log('I am never called, either.');
    });

// Alt 2 - Throw error in onRejected handler
Ember.RSVP.all(allPromises)
    .then(function() {
        console.log('I am called!');
    }, function () {
        console.log('I am never called!');
        throw new Error("boom");
    }).catch(function(err) {
        console.log('I am never called, either.');
    });

Alt 1 will print:
I am never called, either.

Alt 2 will print:
I am never called!
I am never called, either.