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

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

You can use setTimeout and node.send
you can send to multiple outputs with node.send same format as return. To skip a output just send null in the array.
example

[{"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":[]}]

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

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.

[{"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.

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;

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