Make JS query finish before running new line?

I have a very simple inquiry: how do I make the first line run (query29 triggers and finishes running) before the second line runs (getting the results from the query running). I have attached the code below.

query29.trigger();
var currResults = JSON.stringify(query29.data.hits);

What keeps happening is the query29 doesn't finish running before the results are obtained, making the results not up to date.

I have tried await, Promise.resolve, trigger.then(), and they all didn't seem to work. Delay is also not defined in Retool.

Is there a simply solution that I am missing? Thank you in advance.

@WilliamWang

Hey! Not sure what resource you are using, but first wanted to check if you had the query set to run automatically when inputs change? That way it should always have 'current results' and you won't need to trigger it in that code block.

You could also have two separate functions:

  1. Has query29 trigger
  2. Has the stringified data that runs on success of first function

You can then add a success handler on query29 which will ensure it has finished running before you try to stringify the data.

Hope this helps!

Hi Lauren, thank you for the response. This my full code. It is a pure Javascript query:

var index = 0;
var record = [];
for (const element of customItemsTable.data.sku) {
    if (true) {

        var currSku = element;
        var currName = customItemsTable.data.name[index];


        currSearchName.setValue(currName);

        // await query29.trigger();
        var query29 = testAlgoliaSearch;
       // query29.trigger();
         const promise = Promise.resolve(query29.trigger().then(record.push(JSON.stringify(query29.data.hits['0'].sku))));
        // console.log(promise);
      // Delay(1000);
        var currResults = JSON.stringify(query29.data.hits);

        var skuResults = formatDataAsObject(query29.data.hits)['sku'];
        var includes = (formatDataAsObject(query29.data.hits)['sku']).indexOf(currSku);
        textInput3.setValue(currSku);
       // text2.setValue(formatDataAsObject(query29.data.hits)['sku'].toString());
       // record.push(includes);
    }
    index += 1;
}

text1.setValue(record.toString());

// customItemsTable.data.sku.forEach(x => textInput3.setValue(x));

return record;

Yes, Query 29 is set to run automatically when inputs change. The problem is I have to run Query 29 for every element in the table. So unfortunately a success handler (I tried) only works for the first element, and all other elements are blank.

Does my full code snippet help? Thank for the insights Lauren!

@WilliamWang

This is super helpful, thank you! Can we also see a screenshot of query29? Stepping back a little bit, can I ask what you are using this workflow for?

In the meantime, linking this doc in case it is helpful :grinning_face_with_smiling_eyes:

Sure Lauren! I have attached a screenshot.

Query29 (now renamed to algoliaItemSearch) is meant to return the Algolia item results for a certain product name. I am trying to get the results for every single item in the table. Unfortunately, the search query doesn’t finish running before JS obtains the results. This means I end up with the first search query’s result for every single item in the table sadly.

@WilliamWang

Thank you! This is helpful to see. I know you mentioned already trying await and then, but I wanted to ask a couple follow ups. When you used await, did you check this 'Keep variable references inside the query' option in the query's Advanced settings?

When you use then can you try using:

query29.trigger().then(data => record.push(JSON.stringify(data.hits['0'].sku)))

instead of:

Promise.resolve(query29.trigger().then(record.push(JSON.stringify(query29.data.hits['0'].sku))));
1 Like

Hey Lauren, sorry for the late response, I was busy with another task. I will try that today!

Thank you Lauren, it works now! I think the 'Keep variable references inside the query' option was what I was missing. Thank you for your help!

Resolved: just need to add an await to the front.


I am trying to use this pattern to trigger a query a query and then use the return value in the query, however when I run the query it is not waiting for the tagTemplateUpsert query to finish and the return value to be set. When I do the promise.resolve it doesn't it make it wait either. I have checked the 'keep variable references inside the query' option. This is the code I am using to set the tagTemplateValue variable that I then use later.

tagTemplateUpsert.trigger({
additionalScope: {
"tagTemplateObject": tagTemplateObject
}
}).then(data => tagTemplateValue = data[0].id)