# How to properly handle node.done()

**URL:** <https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315>\
**Category:** General\
**Tags:** function-node\
**Created:** [6 April 2023 14:39 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315 "2023-04-06T14:39:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![thalesmaoa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thalesmaoa/32/17067_2.png) [@thalesmaoa](https://discourse.nodered.org/u/thalesmaoa)\
**Post date:** [6 April 2023 14:39 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/1 "2023-04-06T14:39:31Z")

</div>

I'm a little confused about `node.done()` function and how to use it after a for loop.

Let's suppose that I have 10 messages to send async. I must tell nodered the that it has finished due to timeout issues.

In my example, I should only call `node.done()` only after I every message went out, or after every `node.send()`?

---

<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:** [6 April 2023 14:51 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/2 "2023-04-06T14:51:04Z")

</div>

Great question - one that myself, is interested to know.

on the basis of :

> If a Function node does asynchronous work with a message, the runtime will not automatically know when it has finished handling the message.
> 
> To help it do so, the Function node should call `node.done()` at the appropriate time. This will allow the runtime to properly track messages through the system

I would say, your node is doing work with a message - it may send further messages multiple times using async methods - but doing so, its still "doing work".

so i would say after the last `send` - but I don't know the correct answer - someone here will know however

---

<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:** [6 April 2023 14:52 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/3 "2023-04-06T14:52:30Z")

</div>

You should call done when you have finished everything that is required by the message that went in. The purpose of it is so that a user of that node can tell when it has completed acting on the message that was sent in.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 April 2023 15:23 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/4 "2023-04-06T15:23:48Z")

</div>

> [@marcus-j-davies](#):
>
> so i would say after the last `send`

👍

> [@Colin](#):
>
> You should call done when you have finished everything that is required by the message

👍

  

Additionally, calling `done()` signifies that is it complete and ensures the `complete` node operates.

If an error (like a timeout or something occurs) you should call done with the error e.g. `done(err)` and then the `catch` node will permit users to "catch" your error.

  

> **~Ignore me - I assumed wrong~**
>
> > [@thalesmaoa](#):
> >
> > I'm a little confused about `node.done()`
> 
> NOTE: Because you are talking about the `done` function I assume you are talking about a custom node you are developing right?
> 
> Because it is not `node.done` but is infact just `done` (i.e. the callback passed into the 'on' 'input' handler) e.g...
> 
> See: [JavaScript file : Node-RED](https://nodered.org/docs/creating-nodes/node-js#receiving-messages)

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 April 2023 15:34 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/5 "2023-04-06T15:34:43Z")

</div>

> [@Steve-Mcl](#):
>
> If an error (like a timeout or something occurs) you should call done with the error e.g. `done(err)` and then the `catch` node will permit users to "catch" your error.

Hey Steve,  
what is the difference with node.error(...)? Saw yesterday something in the documentation, but it was not really clear to me.

Can I summarize it like this?

- Call `done()` when the input message processing has finished succesfully.
- Call `done(err)` when the input message processing has finished unsuccesfully.
- Call `node.error(err)` when there is an error during the processing but you don't return after the error, so you keep on processing the input message.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 April 2023 15:49 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/6 "2023-04-06T15:49:33Z")

</div>

node.error requires a 2nd parameter (the `msg`) in order for the runtime and editor to know where the error originated. `node.done` does not (it already knows because `done` is passed into the VM).

Operationally, your summary is as good as any guide but to add my recollection of this...  
`node.done()` is the "new" way of signalling an error and also a means of triggering the complete action but should really be used to indicate the overall operation is **done** (either cleanly or with an error).

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 April 2023 15:51 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/7 "2023-04-06T15:51:23Z")

</div>

As it happens, I have just noticed a bug with the JSDoc hint for `node.done`

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/6/d62c49eea32a163e82914b33974bd0efeb093580.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af241afa5e3dfb0ef007cc21fbe1e5e4c7c206d9.png)

The type docs are not correct!  
`node.error` should permit an optional 'error' parameter

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 April 2023 18:51 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/8 "2023-04-06T18:51:09Z")

</div>

> [@Steve-Mcl](#):
>
> `node.done()` is the "new" way of signalling an error

Just to make sure I understand this correctly...  
Is this summary better?

1. Call `done()` when the input message processing has finished succesfully, in case the input message is being processed asynchronously. When the message is being processed synchronously, Node-RED automatically detects when the processing is done.
2. Call `done(err)` when the input message processing has finished unsuccesfully due to a blocking error, i.e. when the message processing is interrupted and stopped. This should be used instead of the old `node.error(err, msg)`.
3. Call `node.error(err)` when there is a non-blocking error during the processing, i.e. when the message processing is not interrupted after the error. After that processing we arrive back at 1 or 2.

---

<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:** [6 April 2023 19:20 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/9 "2023-04-06T19:20:41Z")

</div>

Perhaps just to add to #1 that done is only needed when doing async message processing/sending. It isn't needed if you are only sending a msg at the end of the function node code.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 April 2023 19:46 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/10 "2023-04-06T19:46:01Z")

</div>

> [@TotallyInformation](#):
>
> add to #1 that done is only needed when doing async message processing/sending

I have edited my post to add your remark.  
Or like you english speaking people always say: _"Consider it done"_ 😉

---

<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:** [6 April 2023 19:47 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/11 "2023-04-06T19:47:19Z")

</div>

> [@BartButenaers](#):
>
> Or like you english speaking people always say: _"Consider it done"_

Shouldn't that be "Consider it done()" 🤣

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 April 2023 20:09 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/12 "2023-04-06T20:09:39Z")

</div>

Damn you Julian. Now you have even confused me even more 🥴

- When I process a message _synchronously_, then the Complete node is _ **automatically** _ triggered by Node-RED as soon as the synchronous code is completed.
- When I process a message _asynchronously_, then I need to call `done()` myself in my code because otherwise Node-RED doesn't know when the asynchronous code is completed.

However to be able to start the asynchronous code, I first need to run some synchronous code:

1. I need to validate the message
2. Then I need to enrich the message with some extra data
3. Then I call the asynchronous function --\> in which I need to call `done()`
4. I do some postprocessing (e.g. change the node status, ...)

So how does Node-RED see the difference between synchronous processing of a message, and asynchronous processing (which is also started in the middle of a bunch of synchronous code) 🤯

Got a couple of days ago my first question ever about one of my nodes to make it compatible with the Complete node. So it would be very welcome to get some understanding in how this works, so I can refactor my node to accomplish this...

---

<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:** [6 April 2023 20:18 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/13 "2023-04-06T20:18:20Z")

</div>

> [@BartButenaers](#):
>
> Damn you Julian. Now you have even confused me even more

"Your mission, should you choose to accept it ..." 😁

> [@BartButenaers](#):
>
> So how does Node-RED see the difference

It doesn't need to and doesn't care. All `done()` does is terminate the processing - or rather tells Node-RED that the processing has ended. You don't need to do that when running a straight synchronous function because the function has already ended - there is nothing for Node-RED to do.

When talking about custom nodes, it doesn't hurt to call done, I do that in uibuilder. Mostly because the processing is somewhat hybrid and calling done is safer than not calling it. Just as not calling it in a synchronous mode, calling when similarly in such a mode does no harm, it just isn't necessary. I don't do it in simpler nodes because the processing of the msg is more linear. But I could, without any harm done.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 April 2023 20:39 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/14 "2023-04-06T20:39:47Z")

</div>

- For _synchronous_ processing it doesn't matter whether you call `done()` at the end or not.:

- But for _asynchronous_ processing:

I think I am missing something very simple here...

---

<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:** [6 April 2023 20:41 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/15 "2023-04-06T20:41:57Z")

</div>

Even for synchronous nodes I think that if the node can fail then it should call done, either showing success or failure. That allows the user to use Catch and Complete nodes, and know that one, and only one, of them will always be called.

---

<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:** [6 April 2023 20:44 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/16 "2023-04-06T20:44:24Z")

</div>

> [@BartButenaers](#):
>
> I think I am missing something very simple here

I think that Julian meant not that NR automatically calls done() for you, just that there is no benefit to calling done() as messages pass straight through without delay.

---

<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:** [6 April 2023 20:48 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/17 "2023-04-06T20:48:00Z")

</div>

> [@Colin](#):
>
> Even for synchronous nodes I think that if the node can fail then it should call done, either showing success or failure. That allows the user to use Catch and Complete nodes, and know that one, and only one, of them will always be called.

A fair point.

---

<div class="post-metadata">

**Author:** ![thalesmaoa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thalesmaoa/32/17067_2.png) [@thalesmaoa](https://discourse.nodered.org/u/thalesmaoa)\
**Post date:** [8 April 2023 04:06 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/18 "2023-04-08T04:06:32Z")

</div>

Let me add another question.  
From the above discussion, I was also expecting that `complete` node should be called only when `node.done()` is emitted. However, it gets called from every single `node.send()`. Is that the correct behavior?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 April 2023 06:10 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/19 "2023-04-08T06:10:38Z")

</div>

Sorry, but your question is quite ambiguous & I don't want to pass on bad info.

> [@thalesmaoa](#):
>
> From the above discussion, I was also expecting that `complete` node should be called only when `node.done()` is emitted. However, it gets called from every single `node.send()`.

How many times in the function node is `node.send` called is this particular scenario? By that I mean if you trigger the function ONCE, how many times is `node.send` executed.

Are you actually calling `node.done` anywhere in this function node?

Can you share a minimal flow that demonstrates what you are seeing?

---

<div class="post-metadata">

**Author:** ![thalesmaoa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thalesmaoa/32/17067_2.png) [@thalesmaoa](https://discourse.nodered.org/u/thalesmaoa)\
**Post date:** [8 April 2023 14:17 UTC](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315/20 "2023-04-08T14:17:39Z")

</div>

Sorry, after your message I've revisited my code and I confirm my mistake. Node complete is activated after node.done() call. thx and sorry

[Next page](https://discourse.nodered.org/t/how-to-properly-handle-node-done/77315.md?page=2)
