1

I'm trying to execute all promises at the same time so I can do something when they resolve. There is a (forced) rejected promise but '$q.all()' resolves. I'm missing some '$q.all' behaviour?

Thanks in advance!

        function saveOrder () {

            return ordersSrv.saveOrder(order).then(function(data) {

                console.log('saveOrder OK');
            },
            function(error) {

                console.log('saveOrder KO');
            });
        }


        var aPromises = [saveOrder()];

        $q.all(aPromises).then(function () {

            console.log('OK');

        },
        function (error) {
            console.log('---> error');
        });
sr.u
  • 155
  • 1
  • 8

1 Answers1

2

In a try catch block, when you catch an error and don't rethrow it - it is handled:

try {
   throw Error();
} catch (e) {
    console.log("Error", e);
}
console.log("This log also happens");

It is the same with promises:

Promise.reject(Error())
  .catch(e => console.log("Error", e))
  .then(() => console.log("This log also happens"));

You add a catch handler to your saveError rejection - which means you're handling it. If you want to log and still not handle it - rethrow it:

try {
   throw Error();
} catch (e) {
    console.log("Error", e);
    throw e;
}
console.log("This log doesn't happen");

Or with promises:

Promise.reject(Error())
  .catch(e => { console.log("Error", e); throw e; })
  .then(() => console.log("This log doesn't happen"));
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504