# How to wait until a callback function has completed

**URL:** <https://discourse.processing.org/t/how-to-wait-until-a-callback-function-has-completed/33173>\
**Category:** Beginners\
**Created:** [October 30, 2021, 10:10am UTC](https://discourse.processing.org/t/how-to-wait-until-a-callback-function-has-completed/33173 "2021-10-30T10:10:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 30, 2021, 10:10am UTC](https://discourse.processing.org/t/how-to-wait-until-a-callback-function-has-completed/33173/1 "2021-10-30T10:10:36Z")

</div>

In processing 3  
I have two call back functions, one to select an input file and the other to select an output file. In the setup function I want to call the input file select and then the output file select. But being callback functions they both appear at the same time.

So how can I wait in the setup function until each of these file dialogues has finished before proceeding. Searching around I saw something about a “promise”, is this the answer? If so how do I implement it?

Thanks for reading.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 30, 2021, 11:01am UTC](https://discourse.processing.org/t/how-to-wait-until-a-callback-function-has-completed/33173/2 "2021-10-30T11:01:24Z")

</div>

> [@Grumpy\_Mike](#):
>
> So how can I wait in the **setup()** function until each of these file dialogues has finished before proceeding.

The simplest answer is you shouldn’t chase the **setup()** await path at all.

Rather you should instead create some `boolean` variable which will be set to `true` when both callbacks are fully finished.

Inside the 1st callback you call back the 2nd 1, which will then set the “flag” variable to `true` when finished.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 30, 2021, 11:25am UTC](https://discourse.processing.org/t/how-to-wait-until-a-callback-function-has-completed/33173/3 "2021-10-30T11:25:27Z")

</div>

> [@GoToLoop](#):
>
> Rather you should instead create some `boolean` variable which will be set to `true` when both callbacks are fully finished.

Thanks, but I have tried that and it doesn’t seem to work. I have a “finished” variable set inside the function and after calling the file opening function go into a wait while this finished variable is not false. The function completes but the wait while never ends and so the code hangs.  
I assumed this had something to do with the callback thread not passing back the change in that finished variable.

Or am I misunderstanding what you said?

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 30, 2021, 12:15pm UTC](https://discourse.processing.org/t/how-to-wait-until-a-callback-function-has-completed/33173/4 "2021-10-30T12:15:28Z")

</div>

OK I have managed it.  
It turns out that if I put  
`while(loadCompleat == false) { }`  
It would not work but if I put something in that wait function it would. Like this:-  
`while(loadCompleat == false) {delay(10); }`

I started out printing the loadCompleat variable and that worked but made a mess of the console so changed it to a delay call.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 30, 2021, 12:23pm UTC](https://discourse.processing.org/t/how-to-wait-until-a-callback-function-has-completed/33173/5 "2021-10-30T12:23:18Z")

</div>

As long as you’re calling **delay()** outside the sketch’s “Animation” Thread it’s alright.
