In the following javascript code, why does the error (generated by the assignment to an non-existing object property inside the promise) not get caught by the .catch() on the promise, but does get caught by the window.onunhandledrejection function? I would like to be able to catch errors in my promises using .catch() even if they are not errors I have thought of in advance and thus haven't thought to write code to explicitly call reject().
window.onunhandledrejection = function( error ) {
console.log(error.reason);
};
function test() {
return new Promise( async function( resolve, reject ) {
objectThatDoesntExist.property = 1;
} );
}
test().catch( (error) => { console.log("Caught " + error) } );