# Generate messages with for Loops

**URL:** <https://discourse.nodered.org/t/generate-messages-with-for-loops/52854>\
**Category:** General\
**Created:** [27 October 2021 22:17 UTC](https://discourse.nodered.org/t/generate-messages-with-for-loops/52854 "2021-10-27T22:17:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfuchs1987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tfuchs1987/32/44508_2.png) [@tfuchs1987](https://discourse.nodered.org/u/tfuchs1987)\
**Post date:** [27 October 2021 22:17 UTC](https://discourse.nodered.org/t/generate-messages-with-for-loops/52854/1 "2021-10-27T22:17:27Z")

</div>

Hi Forum,

my msg.payload has an array and i want to run a for-loop times the size of the array length. Every time the loop ends the function should return a message. Is that possible?

I tried the following but this is not working, of course

```auto
for (let i = 0 ; i<msg.payload.length; i++){
    return msg;
}

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 October 2021 22:19 UTC](https://discourse.nodered.org/t/generate-messages-with-for-loops/52854/2 "2021-10-27T22:19:11Z")

</div>

Yes it is possible, but rather than calling 'return' which exits the current function, you would use `node.send(msg)`.

[https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)

---

<div class="post-metadata">

**Author:** ![tfuchs1987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tfuchs1987/32/44508_2.png) [@tfuchs1987](https://discourse.nodered.org/u/tfuchs1987)\
**Post date:** [27 October 2021 22:21 UTC](https://discourse.nodered.org/t/generate-messages-with-for-loops/52854/3 "2021-10-27T22:21:25Z")

</div>

> [@knolleary](#):
>
> node.send(msg)

Thank you so much!

---

<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:** [10 November 2021 22:21 UTC](https://discourse.nodered.org/t/generate-messages-with-for-loops/52854/4 "2021-11-10T22:21:32Z")

</div>

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