# Sequential message send from function node

**URL:** <https://discourse.nodered.org/t/sequential-message-send-from-function-node/1004>\
**Category:** General\
**Created:** [22 June 2018 10:46 UTC](https://discourse.nodered.org/t/sequential-message-send-from-function-node/1004 "2018-06-22T10:46:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [22 June 2018 10:46 UTC](https://discourse.nodered.org/t/sequential-message-send-from-function-node/1004/1 "2018-06-22T10:46:55Z")

</div>

Hi guys,

[The Timer with UI feedback](https://discourse.nodered.org/t/timer-with-ui-feedback/191/7) is being improved.

One of possible improvements (code-wise) is controlling the output via one node (bigtimer in this case), not making workarounds.

[Accordingly to the guide](https://nodered.org/docs/writing-functions.html#sending-messages-asynchronously), following line should just send messages and continue the function execution:

```
node.send([null, {payload: 'manual'}, null]);

```

An attempt to use following code sends just return message. I.e. async send doesn’t work.

```
node.send([null, {payload: 'manual'}, null]);
return([{payload: 10}, {payload: 1}, null]);

```

What’s wrong in node.send usage? Please, help to fix.  
Thank you.

---

<div class="post-metadata">

**Author:** ![PKGeorgiev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pkgeorgiev/32/1325_2.png) [@PKGeorgiev](https://discourse.nodered.org/u/PKGeorgiev)\
**Post date:** [22 June 2018 11:34 UTC](https://discourse.nodered.org/t/sequential-message-send-from-function-node/1004/2 "2018-06-22T11:34:41Z")

</div>

`node.send([null, {payload: 'manual'}, null]);` will work as you expect if it was called from within an async callback.

For example:

```auto
fs = require('fs')
fs.readFile('/etc/hosts', 'utf8', function (err,data) {
  if (err) {
    return;
  }
  node.send([null, {payload: data}, null]);
});

```

This means when readFile completes (it is async function) it will asynchronously send the payload to the next nodes.

This code:

```auto
node.send([null, {payload: 'manual'}, null]);
return([{payload: 10}, {payload: 1}, null]);

```

synchronously sends two messages to the output. These messages will eventually be processed asynchronously by the next (consuming) nodes (not very sure - should be clarified).

Btw I found interesting guide that touches parallel topics: [Node-RED Design Patterns](https://medium.com/node-red/node-red-design-patterns-893331422f42)

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [22 June 2018 11:55 UTC](https://discourse.nodered.org/t/sequential-message-send-from-function-node/1004/3 "2018-06-22T11:55:05Z")

</div>

Thank you, Petar.  
Now I understand that I tried to use wrong functionality. Likely, I need to use syncrhonous or sequential send of messages: one after other in known order.

Is that possible in NR?

---

<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:** [22 June 2018 11:58 UTC](https://discourse.nodered.org/t/sequential-message-send-from-function-node/1004/4 "2018-06-22T11:58:54Z")

</div>

The same docs you linked to in your question show how to send multiple messages: [https://nodered.org/docs/writing-functions.html#multiple-messages](https://nodered.org/docs/writing-functions.html#multiple-messages)

You can either:

1. call `node.send` multiple times:  
`node.send(msg1); node.send(msg2); node.send(msg3);`

2. pass multiple messages to `node.send` in one go (an array of arrays…)  
`node.send([[ msg1, msg2, msg3] ])`

3. or, if this is at the end of your function, return an array of arrays  
`return [[ msg1, msg2, msg3] ]`

---

<div class="post-metadata">

**Author:** ![PKGeorgiev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pkgeorgiev/32/1325_2.png) [@PKGeorgiev](https://discourse.nodered.org/u/PKGeorgiev)\
**Post date:** [22 June 2018 13:02 UTC](https://discourse.nodered.org/t/sequential-message-send-from-function-node/1004/5 "2018-06-22T13:02:25Z")

</div>

@igrowing I've misread the question.

In fact `node.send()` will send your message to the output in async manner and your code will continue with the rest of the function.

May be the confusion comes because of the way node.send threats arrays. If you return an array then every element of that array will be sent to a particular output (and by default the function node has only 1 output). So if you return array of 3 objects your function node need to have 3 outputs. The number of outputs can be configured here:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/4cc0f71646a6943ffe731b5b43b92ecaee526042.png)

The msg at index 0 will be sent to output 1, msg at index 1 goes to output 2 and msg at index 2 goes to output 3:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/85b27e730db1dd20a84154834d646befc750f52e.png)

I've created a simple function to prove the theory:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/513c833e0653a46147491feeffafa206d1fead1a.png)

As you can see on the first picture OUT2 have received two messages - one from node.send() and one from the return statement so `node.send()` is async in nature. OUT3 did not receive anything because you've sent **_null_** and **nulls are silently discarded** :

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/2f7ddbb4f81db94d61a407898d99093299d6b307.png)

In your case you're sending `null` and `{payload: 10}` to **output 1**. Since nulls are discarded then only the second message will be delivered to the next node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/84954f98fe80648543815464dff8aa1e39b002dc.png)

I think that's the reason you think `node.send()` is not async.

On the other hand if you want to return an array of objects **to the first output** you must send an array with **one element**. This one element should be an array of 3 objects (in your case) as @knolleary mentioned. All that depends on what you are trying to achieve.

Hope that helps more 🙂
