1

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');
torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53

1 Answers1

2

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; });
  • So you cannot use normal promises where you dont want to wait for a reply? I thought you could just mix and match. – torbenrudgaard Jun 17 '17 at 10:37
  • Actually you were right @torazaburo - once I put `res = await findProperty();` it worked just fine. My question is then, how to I run all my fire and forget promises? I have a lot where I do not want to wait for a result. – torbenrudgaard Jun 17 '17 at 10:41
  • @torbenrudgaard yes. But then you need to provide a catch like findProperty().catch(console.log.bind(console)); or simply dont reject them – Jonas Wilms Jun 17 '17 at 10:41
  • @Jonasw ahhhh... okieee.. I think I got it. So I give them a separate catch so I can print on console or what ever. Didnt know that you could do this, thanks. What is `console.log.bind`? Never seen bind before? – torbenrudgaard Jun 17 '17 at 10:49
  • @torbenrudgaard passing console.log just passes *log* , if you do that it will say *error, log isnt part of a console instance* , thats why you need to bind console to it. Or you do: .catch(e=>console.log(e)); that does not pass a reference so theres no problem... – Jonas Wilms Jun 17 '17 at 10:51
  • @torbenrudgaard See [Can I fire and forget a promise in nodejs (ES7)?](https://stackoverflow.com/q/32384449/1048572) – Bergi Jun 17 '17 at 11:05
  • @Jonas actually in modern engines `.catch(console.log)` works just fine. –  Jun 17 '17 at 12:56
  • @Jonasw thanks - and Bergi is the promise MASTER :-) – torbenrudgaard Jun 17 '17 at 13:59