Here is JS code:
function createPromise() {
const result = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('my error'));
}, 2000);
});
// uncomment this to get error
// result.finally(() => {
// console.log('finally before');
// });
return result;
}
function test() {
createPromise()
.then(() => {
console.log('promise then');
})
.catch((error) => {
console.log('promise catch:', error);
})
.finally(() => {
console.log('promise finally');
});
}
Its works properly and after executing I have in the console:
promise catch: Error: my error
promise finall
But after uncommenting:
result.finally(() => {
console.log('finally before');
});
In console I have uncaught exception:
finally before
promise catch: Error: my error
promise finally
Uncaught (in promise) Error: my error
Why is this happening and how to explain it?