How to wait until a callback function has completed

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.

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.

1 Like

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?

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.

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