I use the library of promises Q in Angulyar 1. I have this code:

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

The console displays the error throw new Error ("o_O");
And then this error is displayed again console.log (err);

console

I want to display an error only in the catch section.

How to prevent the output of errors by throw inside the then sections?

Code example:
http://plnkr.co/edit/MiH48Qb7tfCEW31mz1ou?p=preview
To reproduce the problem, you must first open the browser console, then run the example and in the console you will see what is displayed first by throw, and then by console.log).

  • one
    but no way. When using native Promise - the conclusion is only one. Try adding a minimal reproducible example - perhaps the error is not related to the library - Grundy
  • Created: plnkr.co/edit/MiH48Qb7tfCEW31mz1ou?p=preview (there you must first open the browser console, then run the example and the console will see what is displayed first by throw and then by console.log) - Khusamov Sukhrob
  • Damn, well, would you at least take a debug version of the library, but not a minified ... - Pavel Mayorov
  • Put the debug - Khusamov Sukhrob Nov.

2 answers 2

It seems to have found a way around this trouble. 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); }); 
  • This option is only for controlled errors, when you yourself decide that the promise should now go along the reject branch. If an unexpected exception occurs, there will still be a double conclusion. - Grundy
  • I indicated this: "at least my own." Unexpected will have to endure)) - Khusamov Sukhrob
  • In general, the use of native Promise is quite a solution for itself - Grundy
  • I do not know how to replace the promiss in the service $ http. If you can somehow globally change that the angular uses another library, then the solution will be ideal. - Khusamov Sukhrob
  • The most clumsy solution: rewrite the service $q . - Grundy Nov.

The $q service uses its own Promise implementation .

In it, the processing process is the processQueue function processQueue

 function processQueue(state) { var fn, deferred, pending; pending = state.pending; state.processScheduled = false; state.pending = undefined; for (var i = 0, ii = pending.length; i < ii; ++i) { deferred = pending[i][0]; fn = pending[i][state.status]; try { if (isFunction(fn)) { deferred.resolve(fn(state.value)); } else if (state.status === 1) { deferred.resolve(state.value); } else { deferred.reject(state.value); } } catch (e) { // обработка исключений возникших в функциях обратного вызова deferred.reject(e); exceptionHandler(e); } } } 

As you can see, if an exception occurs in the callback, then in the catch , two actions are performed:

  1. reject handler execution scheduling
  2. call exceptionHandler

You should pay attention to the second point: this function eventually calls console.error using the $log service. It is this call that writes the error to the console.

Apparently, to avoid output a second time, instead of the $q service, you should use the native Promise.

  • Thanks for the detailed answer! And I was looking for the debug option to turn off error output inside then. - Khusamov Sukhrob
  • @KhusamovSukhrob, not, as I understand it, debug is only responsible for that. whether debug messages will be displayed, errors seem to have nothing to do with it and the behavior cannot be changed. But maybe you should dig into the source more :) or just redefine the $ log service for example :) - Grundy
  • Found a partial solution. It is necessary instead of throw to use return $ q.reject (). - Khusamov Sukhrob