Angular 1.6.0: "Possibly unhandled rejection" error [duplicate] Angular 1.6.0: "Possibly unhandled rejection" error [duplicate] angularjs angularjs

Angular 1.6.0: "Possibly unhandled rejection" error [duplicate]


Try adding this code to your config. I had a similar issue once, and this workaround did the trick.

app.config(['$qProvider', function ($qProvider) {    $qProvider.errorOnUnhandledRejections(false);}]);


The code you show will handle a rejection that occurs before the call to .then. In such situation, the 2nd callback you pass to .then will be called, and the rejection will be handled.

However, when the promise on which you call .then is successful, it calls the 1st callback. If this callback throws an exception or returns a rejected promise, this resulting rejection will not be handled, because the 2nd callback does not handle rejections in cause by the 1st. This is just how promise implementations compliant with the Promises/A+ specification work, and Angular promises are compliant.

You can illustrate this with the following code:

function handle(p) {    p.then(        () => {            // This is never caught.            throw new Error("bar");        },        (err) => {            console.log("rejected with", err);        });}handle(Promise.resolve(1));// We do catch this rejection.handle(Promise.reject(new Error("foo")));

If you run it in Node, which also conforms to Promises/A+, you get:

rejected with Error: foo    at Object.<anonymous> (/tmp/t10/test.js:12:23)    at Module._compile (module.js:570:32)    at Object.Module._extensions..js (module.js:579:10)    at Module.load (module.js:487:32)    at tryModuleLoad (module.js:446:12)    at Function.Module._load (module.js:438:3)    at Module.runMain (module.js:604:10)    at run (bootstrap_node.js:394:7)    at startup (bootstrap_node.js:149:9)    at bootstrap_node.js:509:3(node:17426) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: bar


The first option is simply to hide an error with disabling it by configuring errorOnUnhandledRejections in $qProvider configuration as suggested Cengkuru Michael

BUT this will only switch off logging. The error itself will remain

The better solution in this case will be - handling a rejection with .catch(fn) method:

resource.get().$promise    .then(function (response) {})    .catch(function (err) {});

LINKS: