Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling .finally on a promise that is try/catch handled elsewhere causes UnhandledPromiseRejection in nodejs

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

then, catch, and finally create new promises that are resolved to the promise you call them on and the result of the handler function you pass into them. (If resolved to is slightly unfamiliar, see my blog post on promise terminology.) The unhandled rejection is from the promise created by finally, which was rejected because the promise it was resolved to was rejected. Nothing in your code handles the rejection of that promise, just the other one.

If you want to use finally on the promise created in foo, return the resulting promise instead of the original one:

const foo = () => {
    const promise = new Promise((resolve, reject) => {
        reject('my-error');
    });
    
//  vvvvvv−−−−−−−−−−−−−−−−−−−−−−
    return promise.finally(() => {
        console.log('at foo');
    });
}

const bar = async () => {
    try {
        const result = await foo();
    } catch (e) {
        console.log('at bar: ', e);
    }
}

bar();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading