Queries returning BEFORE all event handlers have exited

I found what appears to be a bug (or at least unfortunate oversight) in query control flow. Say you have 2 queries --
query1:

await new Promise(r => setTimeout(r, 1000));
console.log("ping");

query2:

await query1.trigger();
console.log("done");

and query1 has the following script as an event handler on success:

await new Promise(r => setTimeout(r, 1000));
console.log("pong")

If you run query2, you would expect to see

ping
pong
done

in the console, as query2 blocks on query1 immediately. Then, query1 waits a second before printing "ping". Next, it's success handler fires, waits another second, and prints "pong". Finally, control returns to query2 where it prints "done". Instead, I see

ping
done
pong

in the console because control erroneously returns to query2 before all event handlers on query1 exit. The first control flow scheme should be an option in the trigger method and advanced options to at least avoid breaking existing apps.

Hey @sam-g - thanks for reaching out and for your patience. :slightly_smiling_face:

The actual behavior that you're seeing is expected, as far as I'm aware, but I can certainly understand that there are scenarios where you'd want to await the completion of a query's entire dependency tree. In such a case, the best solution currently is orchestrating that chain of highly dependent, synchronous queries via a single JS query that can then itself be awaited when referenced elsewhere.

I do think it would be interesting to introduce a native solution, though. :thinking: Maybe a parameter that can be passed into the trigger method's options object. I can explore the potential for something like that! In the meantime, feel free to share more about what you're specifically trying to accomplish and we can help coordinate the queries.