Doing async operation for arrays in NodeRed function

483 views
Skip to first unread message

Sukhyung Lee

unread,
Oct 25, 2017, 9:42:55 PM10/25/17
to Node-RED
I'm currently writing a function to parse some incoming JSON data and displaying them as charts.
In order to do that, I'm using a for loop and populating JSON data in an array like this.

var allMsg = [];

async.each (jsonData.dataSet,
  function (sensorItem, callback) {
    for each item in array .... {
      var msg1 = {};
      msg1.req = msg.req;
      msg1.res = msg.res;
      msg1.payload = "some payload";
      allMsg.push(msg1);
    }
    callback();
  },
  function(err) {
    node.send([allMsg];
  }
};
return;
}

Above example only send first element which is allMsg[0].
I know that to handle async operations you need to do node.send as opposed to return msg, but I'm just not quite sure where I should do node.send 
If anyone can help, that'd be super great... Thanks!

Colin Law

unread,
Oct 26, 2017, 3:46:24 AM10/26/17
to node...@googlegroups.com
Why are you using async? Since the work you are doing in the loop
appears to just involve the processor (rather than file I/O or talking
to a sensor for example) and node-red is single threaded then I don't
think you will benefit anything, and it just makes the code more
complex. If what you want is a sequence of messages on the output then
just iterate through the dataset and use node.send for each one as you
complete it.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/0d2e6037-315d-4da8-a691-3c69e35d8e61%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ben Hardill

unread,
Oct 26, 2017, 3:49:50 AM10/26/17
to Node-RED
Please do not cross post to multiple places (I've answered this on Stackoverflow here https://stackoverflow.com/q/46944664/504554), it's very likely to be the same people see it in both places and asking in multiple places just splits the responses.

Sukhyung Lee

unread,
Oct 26, 2017, 5:47:00 PM10/26/17
to Node-RED
Yeah I was just feeling a little lost and wanted to get more response from people.  Thanks for answering on Stackoveflow. I think there are some people that will only see this node-red group rather than Stackoverflow.

Sonic Lee

unread,
Oct 26, 2017, 7:43:24 PM10/26/17
to Node-RED
Normally if you don't use async and iterate through an array, it will not wait until the loop is complete and jump down to return msg.
So that's why I had to use async to make sure the array operation finished completely before returning something.

Colin Law

unread,
Oct 27, 2017, 2:08:24 AM10/27/17
to node...@googlegroups.com
Sorry, that is just not right, it will finish iterating the array before dropping out of the loop.

Colin

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Sonic Lee

unread,
Oct 27, 2017, 3:33:23 AM10/27/17
to Node-RED
Well I don't think it does... it really won't finish the loop before returning.
If you do it without async, you'll get this.
 Error: Can't set headers after they are sent
I think this is inevitable in NodeJS..  i'm pretty sure this is the case
Colin

Colin Law

unread,
Oct 27, 2017, 4:04:49 AM10/27/17
to node...@googlegroups.com
In that case I don't understand what is going on. Can you post the
whole code, I suspect there must be something missing from the snippet
you have posted.

Colin
> https://groups.google.com/d/msgid/node-red/4c979889-ea38-4d8a-85f6-210e06560b5c%40googlegroups.com.

Nick O'Leary

unread,
Oct 27, 2017, 5:36:17 AM10/27/17
to Node-RED Mailing List
Hi,

there really is no need to use async.each here - as Colin says, it complicates the code.

The reason you're getting ` Error: Can't set headers after they are sent` is because you are sending multiple messages to an HTTP Response node with the same msg.res object attached.

When you have an HTTP In -> HTTP Response pair of nodes, the HTTP In node emits a message with msg.req and msg.res attached for each request received. When the HTTP Response node receives a message with those properties, it knows which request to reply to. If it receives a second message with the same instance of those properties, you get the error as it has already responded to the request.

So the issue here is understanding what your wider flow is doing and what behaviour you're trying to achieve.

Nick





> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/4c979889-ea38-4d8a-85f6-210e06560b5c%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

Sonic Lee

unread,
Oct 28, 2017, 9:11:12 PM10/28/17
to Node-RED
Sorry I must have been mistaken. I was just worried about async too much so I lost focus.
It seems I should really pack all my data into one.
Reason I was having multiple msg was to separate each msg into a chart.
> https://groups.google.com/d/msgid/node-red/4c979889-ea38-4d8a-85f6-210e06560b5c%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages