# Is it possible to trigger a flow and wait for it to complete?

**URL:** https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321
**Category:** General
**Created:** [9 September 2019 13:40 UTC](https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321 "2019-09-09T13:40:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [9 September 2019 13:40 UTC](https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321/1 "2019-09-09T13:40:29Z")

</div>

Quick summary : I'd like to trigger a flow from within a function node, then wait for it to complete, either by receiving a message or detecting a change in a context variable.

It may be that the new async mode in v1.0 will help with this, and if so I'm happy to try out the Beta.

More detail : I'm building a number of identical devices based on the ESP8266, and when each powers up it will send its MAC address to a Node-RED based controller. The controller will look up the MAC in a table, and reply with the device's "ID", effectively its MQTT topic. This means I don't have to hard code each device's ID, and if one fails I can substitute another with just a quick edit to the table data file.

However, if a device's message arrives before the table has been loaded into the global context (which might happen after a power outage) I'd like to wait in the lookup function until it's available.

I've tried various flow loops and async functions, but so far can't come up with a workable solution. I've even had occasional infinite loops which needed a controller reboot to get out of 😮

Any suggestions (including updating to v1.0) would be very welcome.

m

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [9 September 2019 13:44 UTC](https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321/2 "2019-09-09T13:44:25Z")

</div>

Have you looked at the 'queue' contrib nodes that are available.  
[https://flows.nodered.org/?term=queue&num\_pages=1](https://flows.nodered.org/?term=queue&num_pages=1)

They halt the flow until a process has completed.

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [9 September 2019 13:50 UTC](https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321/3 "2019-09-09T13:50:31Z")

</div>

Thanks Paul. I'll have a look at those, and it may be possible to do some sort of looped flow which waits for a message from the table loading flow. It would be nice to do it all inside the function, but that may not be possible (I've just started learning Node-RED).

m

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [9 September 2019 13:54 UTC](https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321/4 "2019-09-09T13:54:53Z")

</div>

> [@molesworth](#):
>
> It would be nice to do it all inside the function, but that may not be possible

If it helps, take a look at how this was achieved by @dceejay in this example;  
[https://flows.nodered.org/flow/cea8afa28b7a93ebdc0f](https://flows.nodered.org/flow/cea8afa28b7a93ebdc0f)

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [9 September 2019 14:06 UTC](https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321/5 "2019-09-09T14:06:53Z")

</div>

> [@Paul-Reed](#):
>
> If it helps, take a look at how this was achieved by @dceejay in this example;  
> [Simple triggered queue (flow) - Node-RED](https://flows.nodered.org/flow/cea8afa28b7a93ebdc0f)

Ah, that looks interesting. I didn't know about queues as part of Node.js - I can see I've still a lot to learn about. I'll try to adapt the way this works to use the message from the table loading.

m

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [9 September 2019 14:28 UTC](https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321/6 "2019-09-09T14:28:27Z")

</div>

> [@Paul-Reed](#):
>
> Have you looked at the 'queue' contrib nodes that are available.  
> [https://flows.nodered.org/?term=queue&num\_pages=1](https://flows.nodered.org/?term=queue&num_pages=1)
> 
> They halt the flow until a process has completed.

Thanks! This proved to be a quick and easy solution. I used the "node-red-contrib-queue-gate" node to queue up the device messages, and sent an "open" message from the table load flow. Works a treat 😃

Simples!!

(I'll also take a closer look at the @dceejay flow though, as it looks like I can learn a lot from it.)

m

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [9 September 2019 14:35 UTC](https://discourse.nodered.org/t/is-it-possible-to-trigger-a-flow-and-wait-for-it-to-complete/15321/7 "2019-09-09T14:35:11Z")

</div>

> [@molesworth](#):
>
> used the "node-red-contrib-queue-gate"

Good choice!
