A question about Promises and catch()

I am trying to debug a third party nodejs package. In there is some code like

asyncFunctionReturningPromise()
  .then(/*ok code ...*/)
  .catch();

Note that the catch is empty. If the function throws an exception then this is not caught, and node-red crashes with an uncaught exception. If I replace the catch with

.catch((e) => console.error(`A useful error message`))

Then the error is logged and node-red does not crash. Is this expected behaviour? I had expected an empty catch to silently swallow the exception. This is using nodejs 22, if that is relevant.

The browser expects to have a callback in catch so it needs to be a valid function.

This should work though I think?

asyncFunctionReturningPromise()
  .then(/*ok code ...*/)
  .catch( () => {})

Of course, as you will be using NodeJS v18+, you can also use await:

try {
   await asyncFunctionReturningPromise()
   // do something if it works
} catch (e) { } // ignore an error
2 Likes

Yup.

catch is actually a function, expecting a call back as a parameter, so the catch function being called, its self is failing - trying to execute a callback that has not been passed to it

At least that's how I am seeing it :nerd_face:

In fact the exception that gets to node red is the original exception, not an error from the invalid catch code. So it appears to be just completely ignoring the catch() statement.

That would imply that the library you are using is not correctly handling its own errors. So that would likely be a bug in the library and something you can raise with the author.

If that library is, itself using a promise and hasn't added a catch, I think that would cause what you are seeing.

Have you tried the await version? Does that behave the same way?

It is that library that I have been debugging. ecowitt-gateway - npm
I have already submitted an issue but I wanted to make sure I understood the problem. It is used by node-red-contrib-ecowitt-gateway and the bug can crash node-red if the gateway is not accessible (or in my case hasn't even been delivered yet, it is coming tomorrow, hopefully).
I find it a bit surprising that an empty catch statement is silently ignored rather than itself raising some sort of error.