# How to obtain a sequential out, from multiple outputs in a function node

**URL:** <https://discourse.nodered.org/t/how-to-obtain-a-sequential-out-from-multiple-outputs-in-a-function-node/69575>\
**Category:** General\
**Tags:** function-node\
**Created:** [25 October 2022 14:31 UTC](https://discourse.nodered.org/t/how-to-obtain-a-sequential-out-from-multiple-outputs-in-a-function-node/69575 "2022-10-25T14:31:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lumanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lumanga/32/58334_2.png) [@lumanga](https://discourse.nodered.org/u/lumanga)\
**Post date:** [25 October 2022 14:31 UTC](https://discourse.nodered.org/t/how-to-obtain-a-sequential-out-from-multiple-outputs-in-a-function-node/69575/1 "2022-10-25T14:31:31Z")

</div>

Dear all,  
I'm really in trouble to write the easiest and solid and reliable code to do this job:  
I have an Injecton node that every 1 sec. gives the output.  
I need to write the code ( into the function node, or othe/others node) to have, each 1 sec. an output on only one out of function node each time, and sequentially:

To better clarify:

**At Time 0: OUT from "A", after 1 sec. OUT from "B" , after 1 sec. OUT from "C after 1sec. restart to OUT from "A" and so on...**

![immagine](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3c582fe2224c62ff378a374bb4ab5cb579d48fa.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 October 2022 14:41 UTC](https://discourse.nodered.org/t/how-to-obtain-a-sequential-out-from-multiple-outputs-in-a-function-node/69575/2 "2022-10-25T14:41:33Z")

</div>

You can use [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) and [node.send](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)  
you can send to multiple outputs with node.send same format as return. To skip a output just send null in the array.  
example

```auto
[{"id":"045bc0047a0505c0","type":"inject","z":"452103ea51141731","name":"every 3 seconds","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"A\",\"B\",\"C\"]","payloadType":"json","x":170,"y":1580,"wires":[["f2e806626ac988d7"]]},{"id":"f2e806626ac988d7","type":"function","z":"452103ea51141731","name":"function 13","func":"msg.payload.forEach((num, index) => {\n let output = [null,null,null];\n output[index] = {payload:num}; \n setTimeout(() => {\n node.send(output)\n }, (index*1000))\n \n})\nreturn null;","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":1580,"wires":[["5f0762fc37e816eb"],["5f0762fc37e816eb"],["5f0762fc37e816eb"]]},{"id":"5f0762fc37e816eb","type":"debug","z":"452103ea51141731","name":"debug 96","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":1440,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![lumanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lumanga/32/58334_2.png) [@lumanga](https://discourse.nodered.org/u/lumanga)\
**Post date:** [27 October 2022 15:31 UTC](https://discourse.nodered.org/t/how-to-obtain-a-sequential-out-from-multiple-outputs-in-a-function-node/69575/3 "2022-10-27T15:31:15Z")

</div>

> [@E1cid](#):
>
> `payload":"[\"A\",\"B\",\"C\"]"`

Dear Sir, I tank you very much...

If I wanto also to have the Payload AND the topic on the exit?

payload:["A","B","C"] and TOPIC:["aaa","bbb","ccc"]

to join the "A" with the"aaa" the "B" with "bbb"... etc, to have the 3 output with  
OUTPUT 1= payload "A" and topic "aaa"  
OUTPUT 2 = payload "B" and topic "bbb"  
OUTPUT 3= payload "C" and topic "ccc"  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 October 2022 15:41 UTC](https://discourse.nodered.org/t/how-to-obtain-a-sequential-out-from-multiple-outputs-in-a-function-node/69575/4 "2022-10-27T15:41:03Z")

</div>

Then the array would need to be an array of objects, so you could define both "A" and "aaa". You would add the topic to the output in the code.

```auto
[{"id":"045bc0047a0505c0","type":"inject","z":"452103ea51141731","name":"every 3 seconds","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"payload\":\"A\",\"topic\":\"aaa\"},{\"payload\":\"B\",\"topic\":\"bbb\"},{\"payload\":\"C\",\"topic\":\"ccc\"}]","payloadType":"json","x":190,"y":880,"wires":[["f2e806626ac988d7"]]},{"id":"f2e806626ac988d7","type":"function","z":"452103ea51141731","name":"function 13","func":"msg.payload.forEach((obj, index) => {\n let output = [null,null,null];\n output[index] = {payload: obj.payload,topic: obj.topic}; \n setTimeout(() => {\n node.send(output)\n }, (index*1000))\n \n})\nreturn null;","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":880,"wires":[["5f0762fc37e816eb"],["5f0762fc37e816eb"],["5f0762fc37e816eb"]]},{"id":"5f0762fc37e816eb","type":"debug","z":"452103ea51141731","name":"debug 96","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":740,"wires":[]}]

```

[edit] re-reading your question if you have `["aaa","bbb","ccc"]` in `msg.topic` and `["A"."B","C"]` in `msg.payload`, then you could add the topic with.

```auto
msg.payload.forEach((num, index) => {
    let output = [null,null,null];
    output[index] = {payload: num,topic: msg.topic[index]};   
    setTimeout(() => {
         node.send(output)
    }, (index*1000))
    
})
return null;

```

---

<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:** [26 December 2022 15:41 UTC](https://discourse.nodered.org/t/how-to-obtain-a-sequential-out-from-multiple-outputs-in-a-function-node/69575/5 "2022-12-26T15:41:30Z")

</div>

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