Unhandled promise rejection error in Angular.Js

Every promise in the code is expected to handle promise rejection, ie by adding a catch(...) to a promise in the code. An example is given below.

var somevar = false;

var PTest = function () {

    return new Promise(function (resolve, reject) {

        if (somevar === true)

            resolve();

        else

            reject();

    });

}

var myfunc = PTest();

myfunc.then(function () {

     console.log("Promise Resolved");

}).catch(function () {

     console.log("Promise Rejected");

});

In some cases the unhandled promise error comes even if you have catch in your code.

var somevar = false;

var PTest = function () {

    return new Promise(function (resolve, reject) {

    if (somevar === true)

        resolve();

    else

        reject();

});

}

var myfunc = PTest();

myfunc.then(function () {

     console.log("Promise Resolved");

});

// See the Difference here

myfunc.catch(function () {

     console.log("Promise Rejected");

});

When a Promise is completed with .reject() or an exception was thrown in an async executed code and no .catch() did handle the error the unhandled promise rejection error occurs.

This rejected promise is like an exception that goes up towards the application entry point and causes the root error handler to produce that output.