# A question about Promises and catch()

**URL:** <https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334>\
**Category:** General\
**Created:** [3 January 2025 11:41 UTC](https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334 "2025-01-03T11:41:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 January 2025 11:41 UTC](https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334/1 "2025-01-03T11:41:22Z")

</div>

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

```auto
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

```auto
.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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 January 2025 12:52 UTC](https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334/2 "2025-01-03T12:52:25Z")

</div>

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

This should work though I think?

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 January 2025 13:04 UTC](https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334/3 "2025-01-03T13:04:34Z")

</div>

> [@TotallyInformation](#):
>
> The browser expects to have a callback in catch so it needs to be a valid function.

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 🤓

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 January 2025 14:10 UTC](https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334/4 "2025-01-03T14:10:39Z")

</div>

> [@marcus-j-davies](#):
>
> so the `catch` function being called, its self is failing - trying to execute a callback that has not been passed to it

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 January 2025 17:22 UTC](https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334/5 "2025-01-03T17:22:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 January 2025 22:01 UTC](https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334/6 "2025-01-03T22:01:54Z")

</div>

> [@TotallyInformation](#):
>
> 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.

It is that library that I have been debugging. [ecowitt-gateway - npm](https://www.npmjs.com/package/ecowitt-gateway)  
I have already submitted [an issue](https://github.com/bmdevx/ecowitt-gateway/issues/1) but I wanted to make sure I understood the problem. It is used by [node-red-contrib-ecowitt-gateway](https://flows.nodered.org/node/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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 April 2025 22:02 UTC](https://discourse.nodered.org/t/a-question-about-promises-and-catch/94334/7 "2025-04-03T22:02:51Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
