Help with asynch logic

I have the following JS Code:

Qry_SkyByVendor.data = await new Promise(resolve => {
resolve(Qry_SkyByVendor.trigger());
});

const item_ids_from_db=Qry_SkyByVendor.data.SKU;
const skus_not_in_wms=;

for (const key in csv_data) {
if (csv_data.hasOwnProperty(key)) {
if (!item_ids_from_db.includes(csv_data[key].Item_ID)) {
skus_not_in_wms.push(parseInt(key))
}
}
}

I am just not able to figure out how to get the item_ids_from_db variable to become fully populated before moving on. As a result, I am getting false positives when running the includes method in the for loop. I have spent time looking through the documentation both on and off of Retool and am just completely stumped.

Hey there dsuby!

I obviously don't have exact context on some of this, but from a glance, the first line seems a bit odd. Is Qry_SkyByVendor.trigger() an async function? If that's the case, maybe something like const data = await Qry_SkyByVendor.trigger() could accomplish what you're looking for and wait for trigger() to finish before moving on :blush:

Hello,

Admittedly, I am a new Retool user and also I don't have a ton of javascript experience so when you say something looks a bit odd it makes me wonder if I am doing something wrong. Qry_SkyByVendor is a SQL query that returns data from an Azure DB. The .trigger() is how I thought you would execute the query. Is that not the case? Also, the await does not seem to do the trick. That is something I had tried prior to creating this thread.

@dsuby,
Did you try it this way?

await Qry_SkyByVendor.trigger();  

const item_ids_from_db = Qry_SkyByVendor.data.SKU;
const skus_not_in_wms = [];

for (const key in csv_data) {
    if (csv_data.hasOwnProperty(key)) {
        if (!item_ids_from_db.includes(csv_data[key].Item_ID)) {
            skus_not_in_wms.push(parseInt(key));
        }
    }
}
1 Like

Yes, however, from what I understand from another thread I read await does not work on queries. That is why I ended up wrapping it in this bit of code:

Qry_SkyByVendor.data = await new Promise(resolve => {
resolve(Qry_SkyByVendor.trigger());
});

Are you able to confirm that the code above will block execution of any code below it?

The await should work in your js code block query. Actually, see if this works for you:

const skyByVendorData = await Qry_SkyByVendor.trigger();  

const item_ids_from_db = skyByVendorData.SKU;
const skus_not_in_wms = [];

for (const key in csv_data) {
    if (csv_data.hasOwnProperty(key)) {
        if (!item_ids_from_db.includes(csv_data[key].Item_ID)) {
            skus_not_in_wms.push(parseInt(key));
        }
    }
}

@dsuby,
Just checking in to see if your issue has been resolved.

Yes, the issue has been resolved. Thank you for your help!