# Why does node.send(msg) work, but cannot return msg as normal?

**URL:** https://discourse.nodered.org/t/why-does-node-send-msg-work-but-cannot-return-msg-as-normal/80587
**Category:** General
**Created:** [15 August 2023 19:40 UTC](https://discourse.nodered.org/t/why-does-node-send-msg-work-but-cannot-return-msg-as-normal/80587 "2023-08-15T19:40:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [15 August 2023 19:40 UTC](https://discourse.nodered.org/t/why-does-node-send-msg-work-but-cannot-return-msg-as-normal/80587/1 "2023-08-15T19:40:55Z")

</div>

Bit of a strange one here. I have this block of code below:

```auto
if (msg.payload.event == "single") {
  var touchpanelIDclicked = msg.topic.split("/")[2]
  var screenIDclicked = msg.payload.screen
  var tileIDclicked = msg.payload.tile
  var touchpanel_zone_mappings = global.get("home.light.config.touchpanel_zone_mappings")
  var zoneID = ""
  
  Object.keys(touchpanel_zone_mappings).forEach(key => {
      if (touchpanel_zone_mappings[key].touchpanelID == touchpanelIDclicked &&
          parseInt(touchpanel_zone_mappings[key].screenID) == screenIDclicked) {
          zoneID = touchpanel_zone_mappings[key].zoneID
          msg.topic = "home/light/" + zoneID + "/scene"
          msg.payload = tileIDclicked - 1
          node.warn(msg)
          return msg
      }
  })
  
}

```

It never returns a message, but if I use `node.send(msg)` instead of `return msg;`, it does return a message, as it should.

I have included `node.warn(msg)` to verify the topic/payload of the message, and sure enough it only returns once.

Is there some special reason that `return msg;` cannot run within a forEach code block?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [15 August 2023 19:55 UTC](https://discourse.nodered.org/t/why-does-node-send-msg-work-but-cannot-return-msg-as-normal/80587/2 "2023-08-15T19:55:34Z")

</div>

Because the return inside forEach returns to the forEach like a callback.

Use a regular `for` loop if you want to return early.

---

<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: [29 August 2023 19:56 UTC](https://discourse.nodered.org/t/why-does-node-send-msg-work-but-cannot-return-msg-as-normal/80587/3 "2023-08-29T19:56:04Z")

</div>

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