# Function node and node.done()

**URL:** <https://discourse.nodered.org/t/function-node-and-node-done/82467>\
**Category:** General\
**Tags:** function-node\
**Created:** [30 October 2023 10:26 UTC](https://discourse.nodered.org/t/function-node-and-node-done/82467 "2023-10-30T10:26:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [30 October 2023 10:26 UTC](https://discourse.nodered.org/t/function-node-and-node-done/82467/1 "2023-10-30T10:26:09Z")

</div>

Hi, sorry if this can seem a silly question, but just to be sure I correctly understand it and use it. The code inside the funcion node is syncronous.  
I have a function node that use the input message in a for cycle. Within this cycle I use node.send() for each iteration. Is that correct?

```auto
for (let i = 0; i < msg.payload.length; i++) {
    test={"payload":msg.payload+i};
    node.send([test]);
   //node.done() is not correct to use it
}
context.set("set":msgpayload);
//other code
//node.done() is not needed since return already does it
return [msg]

```

1. Basically, since I'm in a syncronous code, I don't need node.done() at all (neither after node.send(), nor before return). Correct?
2. What if in the aboveexample I don't have return at the end (0 output)? Should I have node.done() or it is not needed as well?
3. If I have function node with the code below, without node.send() and without return. Do I have to use node.done() at the end?

```auto
if (msg.test) global.set("test", msg.payload);
node.done();// is it necessary or not?

```

Thanks

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [30 October 2023 10:31 UTC](https://discourse.nodered.org/t/function-node-and-node-done/82467/2 "2023-10-30T10:31:04Z")

</div>

If you loop through the payload array and send a message for each element (node.send) then you don't need `return msg` (unless you want to pass on the original message as well).

I have never knowingly used node.done() !  
As I understand it, when the code in a function node is all processed, the instance of that node ceases to exist. I don't need to explicitly end it.

Your context.set syntax is wrong. It should be `context.set ("myvariable", msg.payload)`. Though your snippet does not actually use the context variable.

---

<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:** [30 October 2023 11:27 UTC](https://discourse.nodered.org/t/function-node-and-node-done/82467/3 "2023-10-30T11:27:01Z")

</div>

> [@Lupin\_III](#):
>
> Within this cycle I use node.send() for each iteration. Is that correct?

Yes.

> [@Lupin\_III](#):
>
> Basically, since I'm in a syncronous code, I don't need node.done() at all (neither after node.send(), nor before return). Correct?

That is correct. If you used an async function within the node, you might choose to add a done function. Though actually, I don't think I've ever found a need for that. As long as you know that you will be getting later outputs and as long as you aren't feeding too many rapid inputs, I suspect that you don't need done at all though hopefully someone can correct me if I'm wrong there.

> [@Lupin\_III](#):
>
> What if in the aboveexample I don't have return at the end (0 output)? Should I have node.done() or it is not needed as well?

No, it is not needed.

> [@Lupin\_III](#):
>
> If I have function node with the code below, without node.send() and without return. Do I have to use node.done() at the end?

No, not needed there either.

---

<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:** [30 October 2023 11:33 UTC](https://discourse.nodered.org/t/function-node-and-node-done/82467/4 "2023-10-30T11:33:19Z")

</div>

> [@Lupin\_III](#):
>
> Basically, since I'm in a syncronous code, I don't need node.done() at all (neither after node.send(), nor before return). Correct?

Correct, since the function node will not release the processor until it exits at the end (with or without a return statement).

You never actually need to call node.done() in a function node. If you want to use a Complete node to let you catch when an asynchronous function has completed you can use node.done() to signal that it is complete.

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [30 October 2023 12:24 UTC](https://discourse.nodered.org/t/function-node-and-node-done/82467/5 "2023-10-30T12:24:07Z")

</div>

Thanks so much to all. Clear.

---

<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:** [13 November 2023 12:24 UTC](https://discourse.nodered.org/t/function-node-and-node-done/82467/6 "2023-11-13T12:24:34Z")

</div>

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