torbenrudgaard
torbenrudgaard

Reputation: 2561

Unhandled promise rejection from promise called within Try

I have a promise function called findProperty which in this case rejects like this:

reject('ERR: Unknown propertyID or inactive property in call of newBooking');

And this is my handler that calls findProperty:

async function main() {
    var res = "";
    try { 

        findProperty();
        res = await findUser(userEmail);
        res = await findUser(agentEmail);
        res = await getUsers();

    }
    catch(err) {
        console.error(err);
        console.log(" newBooking: " + err);
        callback( { error:true, err } );
    }
}
main();

This causes the following error:

(node:10276) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ERR: Unknown propertyID or inactive property in call of newBooking
(node:10276) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

I dont get this, I got my catch(err) shouldn't that be enough?

Just tried something, this works fine:

   resolve('ERR: Unknown propertyID or inactive property in call of newBooking');

But this gives the error:

  reject('ERR: Unknown propertyID or inactive property in call of newBooking');

Upvotes: 2

Views: 1495

Answers (1)

user663031
user663031

Reputation:

If findProperty returns a promise, you need to await it in order to trigger the failure within the context of the async function; otherwise the rejection disappears into outer space.

To "fire and forget" without waiting, yet catch failures with your try/catch:

findProperty().catch(e => { throw e; });

Upvotes: 3

Related Questions