S
S
Svyatoslav Khusamov2016-11-26 18:06:43
Angular
Svyatoslav Khusamov, 2016-11-26 18:06:43

How to prevent throw errors inside the then sections of the Promise Q library?

I'm using the Q promise library in Angular 1.
I have this code:

$q((resolve, reject) => {
  resolve(1);
}).then(function(ret) {
  throw new Error("o_O");
}).catch(function(err) {
  console.log(err);
});

An error is displayed in the console throw new Error("o_O");
And then this error is re-displayed console.log(err);
82dd14a70204455a9003efd8f344af87.PNG
I want the error to be displayed only in the catch section.
How to prevent throw errors inside then sections?
Code example:
plnkr.co/edit/MiH48Qb7tfCEW31mz1ou?p=preview
To reproduce the problem, you must first open the browser console, then run the example and you will see in the console what is first output by throw, and then by console.log).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Svyatoslav Khusamov, 2016-11-27
@khusamov

Looks like I found a way around this problem. Instead of throw, you can use $q.reject. Then at least their own exceptions will not be duplicated.

$q((resolve, reject) => {
    resolve(1);
}).then(function(ret) {
    //throw new Error("o_O");
    return $q.reject(new Error("o_O"));
}).catch(function(err) {
    console.log(err);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question