# Best practice for use of Done and Error

**URL:** https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063
**Category:** Developing Nodes
**Created:** [13 January 2021 15:00 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063 "2021-01-13T15:00:55Z")
**Posts on this page:** 10
**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: [13 January 2021 15:00 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/1 "2021-01-13T15:00:56Z")

</div>

Is there a best practice recommendation for the use of `done()` and `error()` in contrib nodes? I notice that the email node calls `done()` or `error()` when it is finished (as picked up by Complete and Catch nodes) but never both, whereas the http request node calls `done()` or both `done()` and `error()`. It may even call just `error()` under some circumstances, I am not certain.

**[Edit]** I realise I have been a bit sloppy in my wording above as now `done()` is used both for normal and error conditions. This may be a better way of phrasing the question:  
Is there a best practice recommendation for the triggering of Complete and Catch in contrib nodes? I notice that the email node triggers either Complete or Catch but never both (as far as I can see). The http request node sometimes triggers Complete and sometimes triggers both Catch and Complete. It may even trigger just Catch under some circumstances, I don't know.

---

<div class="post-metadata">

### Author: ![berijan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/berijan/32/35313_2.png) [@berijan](https://discourse.nodered.org/u/berijan)
#### Post date: [13 January 2021 15:28 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/2 "2021-01-13T15:28:22Z")

</div>

I don't know if it's best practice but have You read this info about error handling?  
[https://nodered.org/docs/developing-flows/flow-structure#error-handling](https://nodered.org/docs/developing-flows/flow-structure#error-handling)

[https://nodered.org/docs/user-guide/handling-errors](https://nodered.org/docs/user-guide/handling-errors)

---

<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: [13 January 2021 16:13 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/3 "2021-01-13T16:13:39Z")

</div>

Hi @berijan, welcome to the forum.

I don't think either of those address the issue of whether a node should call `done()` as well as `error()` when there is an error. Or perhaps I missed it.

---

<div class="post-metadata">

### Author: ![berijan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/berijan/32/35313_2.png) [@berijan](https://discourse.nodered.org/u/berijan)
#### Post date: [13 January 2021 16:23 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/4 "2021-01-13T16:23:09Z")

</div>

Ok, I missunderstod the problem but I've seen this and there they send Done with an optional error info  
[https://nodered.org/blog/2019/09/20/node-done](https://nodered.org/blog/2019/09/20/node-done)

" If a node calls `done()` , it will trigger any ‘Complete’ nodes in the workspace that have been configured to target that node. If `done` is called with an error, then it will trigger any ‘Catch’ nodes, as with existing calls to `node.error(err,msg)` ."

After a quick view it looks like You always sen a Done and if an error You send Done(err)

---

<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: [13 January 2021 16:31 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/5 "2021-01-13T16:31:45Z")

</div>

Oh yes, I was forgetting the detail of how it works now. Looking at that blog it seems that the recommended way will either trigger Complete or Catch, but not both, which is what the email node does. That makes the http request node a bit of an anomaly as it sometimes triggers Complete _and_ Catch.

I will edit my initial post to make my question a bit clearer.

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [13 January 2021 16:32 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/6 "2021-01-13T16:32:28Z")

</div>

Just to keep in mind that’s the new way that is not backwards compatible. Pre 1.0 or 1.2 compatibility would mean to use something like:

```auto
(done) ? done(err) : node.error(err);

```

As those older nodered versions don’t have the done function and in the new functions you should only call `node.error()` if for some reason you already called `done()` and an error happens afterwards for example in the on close function or doing async work.  
At least that’s how I understand it.

Edit:  
I also previously had the discussion when to call done in some edge cases here:

> [@Use of msg, send, done in node that can also be triggered by button & more than one input msg to one logical process, when to call done?](https://discourse.nodered.org/t/use-of-msg-send-done-in-node-that-can-also-be-triggered-by-button-more-than-one-input-msg-to-one-logical-process-when-to-call-done/28934):
>
> Hello, little question here: For my sox record node i have the option that the user can choose whether to start recording on an input message or have no input but a button like the inject node instead to start and stop. So i just started implementing the 1.0 things like using send and done to tell the runtime when the node is done recording. So my question is do i somehow have access to the done function when the node was started with the button? As now the record function gets called from the…

---

<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: [13 January 2021 17:18 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/7 "2021-01-13T17:18:52Z")

</div>

Thanks for that.

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [13 January 2021 17:22 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/8 "2021-01-13T17:22:31Z")

</div>

Sorry just saw you actually were one of the people who helped me back in the thread i linked 🙈

---

<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: [13 January 2021 17:34 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/9 "2021-01-13T17:34:19Z")

</div>

I am forever searching for stuff, finding helpful answers and then realising that I wrote the helpful answer myself 🙂

---

<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: [14 March 2021 17:34 UTC](https://discourse.nodered.org/t/best-practice-for-use-of-done-and-error/39063/10 "2021-03-14T17:34:21Z")

</div>

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