How to wait for first query to finish before triggering other

Hi,

How do I wait for one query to finish before triggering the other query? I have read a few docs in the community but none help.

I am not looking for Promise.resolve, I want the first query to finish execution and then the second query to execute. Second query is dependent on data returned by first query.

await query1.trigger() 

// only when query1 finishes I need to run query2.
// data returned from query1 needs to transformed and some math applied on
// then the data is passed to query2.

query2.trigger({
   additionalScope: <data from query1 after transformation>
})

Currently, query2 doesn't wait for query1 to finish even when using await query1.trigger()

Best,
Anand

Hi @Anand,

You can try the below :

query1.trigger({
    onSuccess: () => {
        setTimeout(() => {
            query2.trigger();
        }, 1000); // 1-second delay, adjust as needed
    }
});
2 Likes

Hi @Anand, the most effective approach is to utilize the success events of the query. You can run your dependent query upon the successful execution of your first query.

1 Like

Hey @Anand! Welcome back to the community and thanks for reaching out.

Both of the solutions presented above are valid. :+1: You can choose between them, depending on whether you want to coordinate the queries via a JS code block or the Retool UI.

Don't hesitate to follow up here if you have any additional questions!