I've recently encountered a weird behavior with nodejs. I'll explain with the following example:
let's say we have two functions, foo and bar, foo creates a promise, calls .finally() on it, and returns it. bar on the other hand, wraps the promise with a try/catch.
const foo = () => {
const promise = new Promise((resolve, reject) => {
reject('my-error');
});
promise.finally(() => {
console.log('at foo');
});
return promise;
}
const bar = async () => {
try {
const result = await foo();
} catch (e) {
console.log('at bar: ', e);
}
}
bar();
the output would be:
at foo
at bar: my-error
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "my-error".] {
code: 'ERR_UNHANDLED_REJECTION'
}
From the output, we can see that the error is handled by bar function, yet an UnhandledPromiseRejection error is thrown.
However, if we replace .finally() with .catch() not such error happen.
const foo = () => {
const promise = new Promise((resolve, reject) => {
reject('my-error');
});
promise.catch(() => {
console.log('at foo');
});
return promise;
}
I'm not sure what's happening over here; I'd appreciate it if someone could help me visualize what's happening.
then , catch , and finally create new promises that resolve based on the promise you call and the result of the handler function you pass to them. (If resolution a is a bit unfamiliar, check out my blog post on promise terminology .)
If you want to finally use the promise created in foo , return the resulting promise instead of using catch as follows
const foo = (props) => {
return new Promise((resolve, reject) => {
reject('my-error');
}).finally(() => {
console.log('at foo');
});
}
const bar = async (e) => {
try {
await foo();
} catch (error) {
console.log('at bar: ', error);
}
}
bar();