# Multiple messages in a for cycle

**URL:** <https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473>\
**Category:** General\
**Created:** [31 March 2021 15:32 UTC](https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473 "2021-03-31T15:32:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simone](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Simone](https://discourse.nodered.org/u/Simone)\
**Post date:** [31 March 2021 15:32 UTC](https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473/1 "2021-03-31T15:32:44Z")

</div>

Hi,  
I'm trying to read a sequence of global variables and send a message for each of them.  
This is my code:

```auto
var Reg = "FFFF"; // Address of Register, 16 bit
var RegH = "FF"; //MSB
var RegL = "FF"; //LSB
var Value = "FF"; //Value for Reg
var PL = ["FF","FF"]; //LSB, Value

for (let i = 1; i < 6; i++){
    Reg = global.get('Reg'+i);
    Value = global.get('Val'+i);

    RegH = Reg.substring(0, 2);
    //RegH =parseInt(RegH, 16);

    RegL = Reg.substring(2, 4);
    //RegL =parseInt(RegL, 16);

    //Value =parseInt(Value, 16);

    PL = [RegL,Value];

    msg.pltable= ["MSB: "+RegH,"LSB: "+RegL,"Value: "+Value, i];
    
    msg.command=RegH;
    msg.payload = PL;
    
    return msg;

} 

```

The global variables are named Reg1...Reg5 and Val1...Val5. so I want to get Reg+i and Val+i globals and for each couple send a message.  
The function send only the first couple Reg1, Val1 and stops.  
The globals have data and if manual addressing i, the output message is formed with right values.  
If "return msg;" is outside the for cycle, the function, obviously, sends the last (5th) couple.  
Seems a very symple function but I can't find the mistake.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [31 March 2021 15:37 UTC](https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473/2 "2021-03-31T15:37:08Z")

</div>

Maybe the docs can help ? [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)

---

<div class="post-metadata">

**Author:** ![Simone](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Simone](https://discourse.nodered.org/u/Simone)\
**Post date:** [31 March 2021 16:31 UTC](https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473/3 "2021-03-31T16:31:07Z")

</div>

Docs always helps.  
It's a day that I'm looking in docs and in topics but I'm still with one message.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [31 March 2021 16:32 UTC](https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473/4 "2021-03-31T16:32:35Z")

</div>

If you want to send a message in a loop, you can use `node.send(object)`

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [31 March 2021 17:05 UTC](https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473/5 "2021-03-31T17:05:01Z")

</div>

Which is the line in the docs I pointed at.

---

<div class="post-metadata">

**Author:** ![Simone](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Simone](https://discourse.nodered.org/u/Simone)\
**Post date:** [31 March 2021 19:53 UTC](https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473/6 "2021-03-31T19:53:00Z")

</div>

Many thanks and sorry if I misunderstood your suggestion.

---

<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 April 2021 19:53 UTC](https://discourse.nodered.org/t/multiple-messages-in-a-for-cycle/43473/7 "2021-04-14T19:53:50Z")

</div>

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