# Wait for payload to finish flow before passing another payload

**URL:** <https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171>\
**Category:** General\
**Created:** [20 June 2022 13:55 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171 "2022-06-20T13:55:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [20 June 2022 13:55 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/1 "2022-06-20T13:55:44Z")

</div>

Hello all!  
I would kindly appreciate any help or advice.

I have a flow that takes a payload from an excel file and splits the payload.  
The number of new payloads that are created are dynamic and is not a static number.

Then, each payload goes through a series of nodes a couple of times (with a switch node), gets modified and eventually is written to an SQL table via an MSSQL node.

I would want that each payload in the flow waits for the previous payload to finish going through the entire flow/loop before proceeding on its own. How can that be possible?

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a946fbbffc8c67bcf41d01c1a3b43ddd2b02a016.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [20 June 2022 14:19 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/2 "2022-06-20T14:19:58Z")

</div>

Is this to maintain database integrity or to prevent messages/variables interfering with each other somehow within Node-red - variables passed by reference?

If it's for db integrity, I wonder if you can do something with transactions (I guess MSSQL has transactions - not my db vendor of choice).

How can you identify when a split payload is completely processed?

---

<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:** [20 June 2022 16:13 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/3 "2022-06-20T16:13:15Z")

</div>

You can use the delay node in constant rate mode as a queue. Set the time to be longer than then loop would ever take. Then when a loop completes use that signal to set msg.flush =1 to release the next item.

---

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [21 June 2022 04:39 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/4 "2022-06-21T04:39:55Z")

</div>

Thank you for your response.  
I would greatly appreciate if you could provide a small example to that.  
Also, I join jbudd's question - what would indicate that the item has completed the loop?

I tried adding a delay node as you said and for instance I set it to send 1 msg per 30 seconds. What it actually did is hold all messages and release them all together after 30 seconds.

 ![Screenshot_11](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5280911a23234db40f7454e56c867e72f4b7fdd.png)

Much thanks again.

---

<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:** [22 June 2022 06:15 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/5 "2022-06-22T06:15:00Z")

</div>

> [@milner236](#):
>
> I tried adding a delay node as you said and for instance I set it to send 1 msg per 30 seconds. What it actually did is hold all messages and release them all together after 30 seconds.

Perhaps you did not set it to Rate mode.

Also have a look at node-red-contrib-semaphore which can be used to allow only one message at a time through a section of flow.

---

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [23 June 2022 05:28 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/6 "2022-06-23T05:28:11Z")

</div>

I actually have, but will check again. I will also have a look at your recommendation. Thanks!

---

<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:** [23 June 2022 07:48 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/7 "2022-06-23T07:48:42Z")

</div>

> [@milner236](#):
>
> I actually have

If the messages are coming out of the delay node, set to Rate Limit mode, quicker than the rate specified in the node then I shall be very surprised.

---

<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:** [23 June 2022 12:14 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/8 "2022-06-23T12:14:49Z")

</div>

Here is a simple example just using a delay node in rate limit mode...

```auto
[{"id":"48d660b3a4109400","type":"inject","z":"d5b4a507fb8086e8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":150,"wires":[["e0f9e206681f3504"]]},{"id":"e0f9e206681f3504","type":"delay","z":"d5b4a507fb8086e8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":395,"y":150,"wires":[["e470f1d794e1bef9"]]},{"id":"943543cf7a1958e4","type":"change","z":"d5b4a507fb8086e8","name":"","rules":[{"t":"set","p":"flush","pt":"msg","to":"1","tot":"num"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":485,"y":270,"wires":[["e0f9e206681f3504"]]},{"id":"e470f1d794e1bef9","type":"function","z":"d5b4a507fb8086e8","name":"Do something that takes a while","func":"\n\nsetTimeout(function() { node.send(msg)}, 3000)\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":180,"wires":[["943543cf7a1958e4","690a432132f0cc08"]]},{"id":"690a432132f0cc08","type":"debug","z":"d5b4a507fb8086e8","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":105,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![milner236](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@milner236](https://discourse.nodered.org/u/milner236)\
**Post date:** [23 June 2022 14:35 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/9 "2022-06-23T14:35:59Z")

</div>

Thank you for your example. Oddly enough, the delay node in rate limit just sort of holds all of my payloads and then lets them go after the timer sets.

Either way, I have made some digging and found out my issue has another root:

If I generate X number of payloads in my flow, is there a way to sort their order in the flow? That may solve my case.

---

<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:** [23 June 2022 19:48 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/10 "2022-06-23T19:48:25Z")

</div>

Do you mean that you have a sequence of messages and you want to re-order them, so later ones overtake earlier ones in the flow? That is complex as first you would have to collect them all together, sort them, and then send them on. Can you change the order in which they appear instead?

[Edit] Looking back I see you posted

> [@milner236](#):
>
> I have a flow that takes a payload from an excel file and splits the payload

Can you sort them before passing them on as a sequence of messages?

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [23 June 2022 21:36 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/11 "2022-06-23T21:36:15Z")

</div>

> [@Colin](#):
>
> Also have a look at node-red-contrib-semaphore which can be used to allow only one message at a time through a section of flow.

I concur with Colin on this... one of my favorite contributed nodes! And actually, it is configurable to allow any number of "in-flight" messages, not just 1 at a time. You can set the pool of tokens to any number, and once they are given out, the rest of the requests wait in line until another `msg` returns its token to the pool.

Works well with very little overhead -- as long as every node that takes a token eventually puts it back (hint: use a catch node for any nodes inside your loop that could throw an exception).

If you output an array of lines from your `csv` node, then use a `sort` node to rearrange the lines =\> `split` them into separate messages =\> push them all into the `semaphore take` node =\> process one at a time =\> use `semaphore leave` to return the token, you should be guaranteed to process them in order one at a time.

---

<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:** [24 June 2022 08:45 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/12 "2022-06-24T08:45:26Z")

</div>

One thing I advise is to have an inject node connected to the semaphore release node so that when it locks up you can easily set it going again. If you do a partial redeploy whilst a message is in transit then this lockup can happen, as the semaphore has been acquired but will not be released again as node red was re-deployed. The alternative is to do a full redeploy if this happens.

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [24 June 2022 09:28 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/13 "2022-06-24T09:28:01Z")

</div>

There are some nodes, which takes an incoming message and puts it in a queue. Then the single messages are processed sequentially. So message 2 is processed only after message 1 has been processed.

> **[node-red-contrib-loop](https://flows.nodered.org/node/node-red-contrib-loop)**
>
> Node-RED nodes to looping (fixed steps, condition based or iterating over arrays, objects, maps, sets, string).

[https://flows.nodered.org/flow/43501a1b424434de0ffb](https://flows.nodered.org/flow/43501a1b424434de0ffb)

> **[node-red-contrib-serial-iterator](https://flows.nodered.org/node/node-red-contrib-serial-iterator)**
>
> Iterate over an Array received on the input, giving the next element only after it receives a feedback

---

<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:** [24 June 2022 10:04 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/14 "2022-06-24T10:04:43Z")

</div>

I suspect they will all suffer from the same problem with partial deploys.

The node-red-contrib-serial-iterator is no longer supported (at least its github page does not exist).

I have no experience with the loop node so I cannot comment on that one.

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [24 June 2022 10:33 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/15 "2022-06-24T10:33:10Z")

</div>

What exactly would be the problems of partial deploys from your point of view? I would be very interested in this so I can try it out on my end.

I only found the three nodes in a 10 second search. Maybe there are more recent versions or implementations.

A similar implementation could also be created with the function node. Using the context, the queue can even be persisted if needed.

I only wanted to show an alternative to the implementation with waiting times with the nodes. I don't like the approach of assuming a fixed estimated runtime as a delay. Either you wait unnecessarily to continue the flow, or the wait time is not sufficient. Either way, waiting based on an estimated wait time is inefficient and unreliable.

---

<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:** [24 June 2022 10:43 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/16 "2022-06-24T10:43:33Z")

</div>

As I said, I would use the semaphore node for this, but the loop node looks as if it should work. One advantage of the semaphore is that you can have multiple end points to the protected section by using multiple release nodes. That can make the wiring less complex when it comes to using Catch nodes for when a node in the protected section throws an error of some sort. The catch can be fed directly into a release node rather than having to be fed back round to the start of the loop. For your case it may be that all you need to do is to add a semaphore acquire node at the start of the flow to be protected, a release node at the end, and catch any errors that may occur on the way through and direct those to release nodes too.

The problem with partial deploys is that once a message enters the loop (or the semaphore protected section) then no more can be released from the queue till that message is complete. If a partial deploy occurs then the message currently in the loop may get discarded so it never gets to the end in order to release the next message. I have not found it a big deal, just something to bear in mind.

---

<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:** [24 June 2022 12:24 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/17 "2022-06-24T12:24:38Z")

</div>

Using the delay node as I showed may get around this as there is a built in timeout in the delay node - so that as long as that isn't the node being partial deployed then it will always timeout at the max time you set and free up for the next item... no need for these extra nodes as far as I can see 😉 (unless you really need multiple semaphores active at once...)

---

<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:** [24 June 2022 12:37 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/18 "2022-06-24T12:37:20Z")

</div>

Yes, if you know there is a maximum time that is required for a message to be actioned, and are happy for all messages to be handled at this slow rate, then a Delay node in Rate Limit mode will do the job. Is that what you meant @dceejay?

---

<div class="post-metadata">

**Author:** ![NetHans](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nethans/32/2843_2.png) [@NetHans](https://discourse.nodered.org/u/NetHans)\
**Post date:** [24 June 2022 12:52 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/19 "2022-06-24T12:52:30Z")

</div>

@Colin  
Thanks for the clarifications on the concerns with partial deploys. A node for loops would of course must have a timeout function integrated to process the next message in case of an error.  
I had read over the suggestion with the semaphore node. I have to take a close look at the node and try it out. What I do not find so nice about the node, that again a dependence with the node is added. The loop nodes get along with correct programming without additional dependencies.

@dceejay  
The delay node would have to pass a function in the msg object, which can tell the delay node that it can process the next message ahead of time. Thus the delay in the node would be a kind of timeout. This would make it possible to control further message processing in the flow. Continuous waiting until the delay has expired could be avoided, which would make the flow more efficient.  
In addition, the user would have the choice. Either he always waits for the delay and does not call the function from the msg object, or he actively controls the further message flow via the function in the msg object.

---

<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:** [24 June 2022 12:54 UTC](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171/20 "2022-06-24T12:54:33Z")

</div>

> [@NetHans](#):
>
> What I do not find so nice about the node, that again a dependence with the node is added. The loop nodes get along with correct programming without additional dependencies.

I don't understand what you mean by that. Can you explain in more detail?

[Next page](https://discourse.nodered.org/t/wait-for-payload-to-finish-flow-before-passing-another-payload/64171.md?page=2)
