# Function return not work

**URL:** https://discourse.nodered.org/t/function-return-not-work/65267
**Category:** General
**Created:** [18 July 2022 13:32 UTC](https://discourse.nodered.org/t/function-return-not-work/65267 "2022-07-18T13:32:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)
#### Post date: [18 July 2022 13:32 UTC](https://discourse.nodered.org/t/function-return-not-work/65267/1 "2022-07-18T13:32:25Z")

</div>

Hi all, I'm writing a function in func-exec node

```auto
msg.topic = ""
var array = [];

var exec = child_process.exec('pgrep firefox', (error, stdout, stderr) => {
    if (error) { 
        console.info("false")
        msg.payload = false 
        msg.topic = "error connection ui"
        return msg;
    } 
    if (stdout != ""){
        var splitString = stdout.split(" ")
        for (var i = 0; i < splitString.length; i++){
            array.push(parseInt(splitString[i]))
        }
        console.info(array)
        msg.payload = array
    }
}); 
console.info(msg.payload)
return msg;

```

unfortunately the msg.payload is always undefined while array is corrected not empty as I can see on the console.

can some help me to understand where I wrong?  
many thanks

---

<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: [18 July 2022 13:42 UTC](https://discourse.nodered.org/t/function-return-not-work/65267/2 "2022-07-18T13:42:10Z")

</div>

does the second if () require a return?

---

<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: [18 July 2022 13:44 UTC](https://discourse.nodered.org/t/function-return-not-work/65267/3 "2022-07-18T13:44:12Z")

</div>

Hi @denissanga

You cannot use `return` to send a message from inside a callback function - you can only use `return` at the top level of the code.

To send a message from inside a callback, you need to use `node.send(msg)` - [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)

---

<div class="post-metadata">

### Author: ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)
#### Post date: [18 July 2022 13:50 UTC](https://discourse.nodered.org/t/function-return-not-work/65267/4 "2022-07-18T13:50:27Z")

</div>

many thanks

I try with this

```auto
msg.topic = ""
var array = [];

var exec = child_process.exec('pgrep firefox', msg, (error, stdout, stderr) => {
    if (error) { 
        console.info("false")
        msg.payload = false 
        msg.topic = "error connection ui"
        node.send(msg)
    } 
    if (stdout != ""){
        var splitString = stdout.split(" ")
        for (var i = 0; i < splitString.length; i++){
            array.push(parseInt(splitString[i]))
        }
        console.info("out1 " + array)
        msg.payload = array
        node.send(msg)
    }
}); 

console.info("out2 " + msg.payload)
return msg;

```

but I obtain  
out1 123345  
out2 true

---

<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: [18 July 2022 13:51 UTC](https://discourse.nodered.org/t/function-return-not-work/65267/5 "2022-07-18T13:51:55Z")

</div>

> [@denissanga](#):
>
> but I obtain  
> out1 123345  
> out2 true

What do you expect to get?

If you only want it to send a message once the `exec` has finished, then delete the `return msg` line at the end.

---

<div class="post-metadata">

### Author: ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)
#### Post date: [18 July 2022 13:59 UTC](https://discourse.nodered.org/t/function-return-not-work/65267/6 "2022-07-18T13:59:46Z")

</div>

I would obtain 123345...there is no way with this approach?

---

<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: [18 July 2022 14:05 UTC](https://discourse.nodered.org/t/function-return-not-work/65267/7 "2022-07-18T14:05:50Z")

</div>

> [@denissanga](#):
>
> .there is no way with this approach?

I do not understand what you mean.

You have said you are getting:

```auto
out1 12324123

```

That tells us the `console.info` you have inside the callback is being called... so we can assume the next two lines are also being called which will cause a message to be sent:

```auto
msg.payload = array
node.send(msg)

```

Have you connected a Debug node to the output of the Function to see what message it is sending?

---

<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: [16 September 2022 14:06 UTC](https://discourse.nodered.org/t/function-return-not-work/65267/8 "2022-09-16T14:06:06Z")

</div>

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