Why is this rejected RSVP promise not calling catch ()?
In my Ember RSVP code, I cannot get any error handlers, although I can see that my "reject" function succeeds. 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 "rejected!" message in the console. What have I done here? Should the () fire go on since reject () was working correctly?
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 say, "Sometimes you might want to work with many promises at once. If you pass an array from promises to the all () method, it will return a new promise that will be fulfilled when all the promises in the array are fulfilled; or rejected immediately, if any." or the promise in the array is rejected. '
source to share
You will catch the error in your handler onReject
. If you want yours to catch
run, you either have to supply a handler onReject
or throw an error in your rejection 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.
Alt 2 will print:
I never called! They never call me.
source to share