# Node-red stuck on while loop in function node

**URL:** <https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077>\
**Category:** General\
**Created:** [5 August 2018 18:31 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077 "2018-08-05T18:31:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![RingoAl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ringoal/32/1572_2.png) [@RingoAl](https://discourse.nodered.org/u/RingoAl)\
**Post date:** [5 August 2018 18:31 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/1 "2018-08-05T18:31:18Z")

</div>

Hello.  
I've such test flow for async actions in function node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4385d215197cad1a1c6f9b03d9f3b67972dbaaa5.png)  
Function code is:

```auto
    var success=0;
    node.send([{"payload":1},null]);
    do {
        success=flow.get("success");
    } while (!success) 
    return [null,{"payload":2}];

```

If I inject a timestamp, node-red will stuck, cause 100% cpu usage.  
I've tried to add a delay by setTimeout, but it doesn't help.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 August 2018 18:46 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/2 "2018-08-05T18:46:32Z")

</div>

Sooooo, where do you store anything in the flow variable?

---

<div class="post-metadata">

**Author:** ![RingoAl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ringoal/32/1572_2.png) [@RingoAl](https://discourse.nodered.org/u/RingoAl)\
**Post date:** [5 August 2018 19:12 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/3 "2018-08-05T19:12:12Z")

</div>

In the Change node at the right.  
Message from inject will pass function node and then go through first output via node.send(). After that function have to wait until flow.success will be changed after delay, and send a message to second output

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 August 2018 19:27 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/4 "2018-08-05T19:27:29Z")

</div>

I may be wrong but I believe Node-RED is single threaded. When it first starts up and the inject node is fired the function runs. Now at this point flow.success has not been set, but you are grabbing it.

It could be that what ever is returned will evaluate to TRUE causing the loop to run forever...

---

<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:** [5 August 2018 19:31 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/5 "2018-08-05T19:31:51Z")

</div>

Node.js is single threaded. Putting a tight loop in your function node will tie up that single thread so nothing else can happen; your flow context value will never change.

You could use setInterval to poll the value at regular intervals and use node.send() to send from within the interval function. You'll also need to remember to clear the interval when done with it.

---

<div class="post-metadata">

**Author:** ![RingoAl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ringoal/32/1572_2.png) [@RingoAl](https://discourse.nodered.org/u/RingoAl)\
**Post date:** [6 August 2018 06:35 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/6 "2018-08-06T06:35:40Z")

</div>

I've rewrited function in such way:

```auto
    var success = 0;
    node.send([{ "payload": 1 }, null]);
    var timerId = setTimeout(function check() {
        success = flow.get("success");
        if (!success) { timerId = setTimeout(check, 50); } else {
            flow.set("success", 0);
            node.send([null, { "payload": 2 }]);
        }
    }, 50);

```

Works fine.  
Thanks.

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [17 October 2018 01:50 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/7 "2018-10-17T01:50:05Z")

</div>

Hello,

I wonder however if there is still a problem with Node Red and the 'while' and 'do while' functions. I have a node that parses (string, text) weather information I get as an ATOM feed (because Open Weather is never up to date for my area) and the following simple loop freezes Node Red every time (must open a CLI and stop/restart Node Red). I'm just trying to remove any sentences about wind speeds. BTW, just upgraded distro to stretch, happens same as w/ Jessie. NR is all up to date.

var endtag, cutout, content = msg.payload; // msg is the ATOM feed  
while (content.indexOf("Wind" \> -1))  
{  
endtag = content.indexOf(".", "Wind");  
cutout = content.slice("Wind", endtag + 1);  
content = content.replace(cutout, "");  
}

Why does 'while' freeze NR with this?  
TIA

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 October 2018 06:03 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/8 "2018-10-17T06:03:10Z")

</div>

Insert node.warn statements in the loop to see what the values are and no doubt it will become clear.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [17 October 2018 06:36 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/9 "2018-10-17T06:36:39Z")

</div>

Do you have an example msg that can be fed into the function ?

---

<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:** [17 October 2018 07:22 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/10 "2018-10-17T07:22:45Z")

</div>

Hi,

there are a number of problem with your code that means it is not doing what you think it is doing and will be stuck in the while loop forever.

```auto
var endtag, cutout, content = msg.payload; // msg is the ATOM feed
while (content.indexOf("Wind" > -1))

```

Your condition in the while loop is `content.indexOf("Wind" > -1)` - you have the `> -1` _inside_ the argument passed to the `indexOf` function. It should be `content.indexOf("Wind") > -1`. That is why your while loop repeats forever.

```auto
{
endtag = content.indexOf(".", "Wind");

```

The `indexOf` function can take two arguments, but the second argument is expected to be the character position to start searching from. You are passing `Wind` as that second argument, so I'm not sure what result you'll get.

```auto
cutout = content.slice("Wind", endtag + 1);

```

The `slice` function also takes two arguments - the start position and the end position. Again, you are passing in the string `Wind` as the start position, rather than a number.

```auto
content = content.replace(cutout, "");
}

```

What exactly is the goal of the while loop? Is it to remove all instances of the word `Wind` from msg.payload? If so, you can do it without a while loop using:

```auto
msg.payload = msg.payload.replace(/Wind/g,"");
```

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [17 October 2018 13:02 UTC](https://discourse.nodered.org/t/node-red-stuck-on-while-loop-in-function-node/2077/11 "2018-10-17T13:02:58Z")

</div>

Sometimes you spend so much time on something you get lost in it. This drove me nuts for some time, especially the lengthy effort for each NR / freeze reboot.

The objective is to remove all sentences that begin with "Wind", up to the next "." after "Wind". The reason for this is I have all output of my NR processes (weather updates, weather alerts, you left the garage door open for \>2hrs, alarm system, solar alarm, etc..) result in a spoken message (Cepstral) over ceiling speakers throughout the house. Having "Wind" info included in the weather announcement when I construct 'today/ tonight/ tomorrow/ tomorrow night and next day' weather info was too much, too long and too annoying. Here is what I get now .....

"Good morning everyone. Here is the latest weather information. Today. Mainly cloudy with 60 percent chance of showers. High 10. For tonight. Mainly cloudy with 60 percent chance of showers this evening. Clearing near midnight. Low minus 2 with frost. Tomorrow. Mainly sunny. High 7. Tomorrow night: Clear. Low plus 4. For Friday. A mix of sun and cloud with 30 percent chance of showers. High 15. End of weather update."

You guys are awesome!. knolleary, thank you. 🙂
