# Flow works unreliably

**URL:** <https://discourse.nodered.org/t/flow-works-unreliably/73260>\
**Category:** General\
**Created:** [5 January 2023 20:28 UTC](https://discourse.nodered.org/t/flow-works-unreliably/73260 "2023-01-05T20:28:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trexnickel](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@trexnickel](https://discourse.nodered.org/u/trexnickel)\
**Post date:** [5 January 2023 20:28 UTC](https://discourse.nodered.org/t/flow-works-unreliably/73260/1 "2023-01-05T20:28:00Z")

</div>

I am doing an _operation_ which consists of sending an api request from a function node to a http node. The _operation_ modifies a label on a remote resource. Due to some limitation the modification _operation_ cannot be done directly but consists of removing the existing label and adding a new label. This corresponds to sending two requests to the http node - first a deletelabel followed by addlabel:

```auto
for (var count = 0; count < num_resources; count++) {
  //example url
  msg.url = "https://myresourceurl/";

  //delete existing label
  msg.payload = JSON.stringify(del_label_jsonobj);
  node.send(msg);

  //add new label
  msg.payload = JSON.stringify(newlabel_jsonobj);
  node.send(msg);
}

```

The issue is that its not doing it reliably. If there are 10 resources to modify, these are the behaviours I have observed:

1. Most of the resources have both the newlabel and the old label present (which means deletelabel did not work but addlabel worked)
2. Few labels have only modified label (which means both the operation worked)

The code seems to be ok since it works 100% reliably if the addlabel and deletelabel are done individually. It's the combination of two operation that does not work. Any insight/help is appreciated.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [5 January 2023 20:47 UTC](https://discourse.nodered.org/t/flow-works-unreliably/73260/2 "2023-01-05T20:47:25Z")

</div>

Hi @trexnickel and welcome to the Forums.

I think maybe you're experiencing some race conditions.  
`node.send` is an async method, so will not wait before continuing down your code block.

Therefore the web service maybe processing `newlabel_jsonobj` first (as you don't wait for `del_label_jsonobj` to finish)

I would also be careful if modifying `payload` whillst using async methods, as the `payload` can be modified whilst its in transit.

See attached for a possible alternative

```auto
[{"id":"51decd3bab97be26","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"01d0828a5e57a822","type":"function","z":"51decd3bab97be26","name":"function","func":"for (var count = 0; count < num_resources; count++) {\n const Message = {\n url: \"https://myresourceurl/\",\n payload: JSON.stringify(del_label_jsonobj),\n add:JSON.stringify(newlabel_jsonobj)\n }\n node.send(Message)\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":250,"wires":[["5cff1d01fa3bbbb1"]]},{"id":"5cff1d01fa3bbbb1","type":"http request","z":"51decd3bab97be26","name":"Process Delete","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":450,"y":250,"wires":[["255e8f1f7c24274b"]]},{"id":"902dc7389078ecf2","type":"http request","z":"51decd3bab97be26","name":"Process Add","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":885,"y":250,"wires":[[]]},{"id":"255e8f1f7c24274b","type":"change","z":"51decd3bab97be26","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"add","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":245,"wires":[["902dc7389078ecf2"]]}]

```

This method will guard against modifying the object, whilst its in use and will wait for the response from the delete request before adding

---

<div class="post-metadata">

**Author:** ![trexnickel](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@trexnickel](https://discourse.nodered.org/u/trexnickel)\
**Post date:** [5 January 2023 21:49 UTC](https://discourse.nodered.org/t/flow-works-unreliably/73260/3 "2023-01-05T21:49:36Z")

</div>

Hi @marcus-j-davies. Thanks for pointing out the possible race condition. I believe that was the issue. Also the possibility of modification of payload before the remote web server processes the request. After incorporating the alternative, it worked! Thank you very much for your help:)

---

<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:** [19 January 2023 21:50 UTC](https://discourse.nodered.org/t/flow-works-unreliably/73260/4 "2023-01-19T21:50:28Z")

</div>

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