I have the following query which triggers an sql statement. I want to wait for all those statements.
I tried by adding delay in the below query, but that is not consistent.
I am using the following in the button:
await sqlQueryTrigger.trigger();
sqlQueryTrigger is a js as shown below:
const dataArray = parsedJSON.value;
if (dataArray && dataArray.length > 0) {
// Define the delay in milliseconds
const delay = 5000; // 1000 milliseconds = 1 second
// Iterate through the dataArray and generate SQL update statements
dataArray.forEach((row, index) => {
// Use setTimeout to introduce a delay
setTimeout(() => {
updateTable.trigger({
additionalScope: {
quote_adjustment: row.quote_adjustment,
Quotation_no: row.Quotation_no,
SKU: row.SKU,
}
});
}, index * delay); // Increase the delay for each iteration
});
}
ScottR
January 4, 2024, 3:25pm
2
You don't need the set timeout, it can be removed.... add await
before updateTable.trigger()
Also as I mentioned in your other post, you can also look at using Promises....
1 Like
I did look at Promises, but couldn't find proper way for implementing it
ScottR
January 4, 2024, 4:35pm
4
Did you implement await in the code instead of in the button?
khatanaashish:
const dataArray = parsedJSON.value;
if (dataArray && dataArray.length > 0) {
// Define the delay in milliseconds
const delay = 5000; // 1000 milliseconds = 1 second
// Iterate through the dataArray and generate SQL update statements
dataArray.forEach((row, index) => {
// Use setTimeout to introduce a delay
setTimeout(() => {
updateTable.trigger({
additionalScope: {
quote_adjustment: row.quote_adjustment,
Quotation_no: row.Quotation_no,
SKU: row.SKU,
}
});
}, index * delay); // Increase the delay for each iteration
});
}
try this:
const dataArray = parsedJSON.value;
if(dataArray && dataArray.length > 0) {
return Promise.all(dataArray.map((item) => {
return updateTable.trigger({
additionalScope: {
quote_adjustment: row.quote_adjustment,
Quotation_no: row.Quotation_no,
SKU: row.SKU,
}
}));
}
}
throw new Error("dataArray is null");
alternatively you can set the result of .map to a variable then return Promise.all()
if(dataArray.....){
let promises = dataArray.map(.......);
return Promise.all(promises);
}
there's an amazing post by @joeBumbaca covering promises in Retool and here is where you can find more details in the Retool docs.
1 Like
@ScottR , that await works but from 2nd time onwards. It fails to load all transformers in the 1st go.
Ultimately, I had to combine all the 5 scripts into 1 and then the problem was resolved.
Thanks!