Function node and node.done()

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?

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?
if (msg.test) global.set("test", msg.payload);
node.done();// is it necessary or not?

Thanks

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.

Yes.

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.

No, it is not needed.

No, not needed there either.

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.

Thanks so much to all. Clear.

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