3

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.'

J B
  • 390
  • 1
  • 4
  • 16
  • 1
    No, the `I am never called, either` should really never be called, as any exceptions are handled by the `I am never called` already. But if you don't get that, there is something wrong indeed. – Bergi Nov 28 '14 at 09:15
  • Do you have the Ember Inspector plugin installed? Have a look at the promises tab on that and see what's going on. You can label your promises as described at https://github.com/emberjs/ember-inspector/wiki/Promises-Tab to make it easier to follow. – Ruaidhrí Primrose Nov 28 '14 at 11:53
  • Sorry, never used Ember.js - but are you 100% sure `Ember.RSVP` is actually there? Played here with raw RSVP (Node.js test-case), that's the only difference I can possibly see between my (working) example and yours. – bardzusny May 29 '15 at 17:57

1 Answers1

1

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.

gabrielf
  • 2,210
  • 1
  • 19
  • 11