Help

I wrote the following code in the automation script and it seems to give me an error which should not happen as per JS docs

Topic Labels: Automations
598 1
cancel
Showing results for 
Search instead for 
Did you mean: 
FinAll_Core_Tea
5 - Automation Enthusiast
5 - Automation Enthusiast

This is the code, all variables exist in this but it says can only use await on async functions:

fetch(“https://api.onfinance.in/insights”, requestOptions)
.then(response => response.text())
.then((result) => {
for(let j=0; j<checkedDataIDS.length; j++){
await table.deleteRecordAsync(checkedDataIDS[j]);
}
console.log("Processed Insights: ", checkedDataIDS);
})
.catch(error => console.log(‘error’, error));

1 Reply 1

That’s because you are only allowed to use await in async functions. You use await when you delete the record in the anonymous function in your second “then”.

It is probably easier to understand if you use a different syntax.

async function doStuff() {
const response = await fetch(“https://api.onfinance.in/insights”, requestOptions)
const result = await response.text()

for(let j=0; j<checkedDataIDS.length; j++) {
await table.deleteRecordAsync(checkedDataIDS[j]);
}
}

You would also be better off submitting the delete requests in batches, instead of one at a time.