Nov 26, 2024 07:39 PM - edited Nov 28, 2024 02:05 AM
In my automation script I am running a fetch request. In some cases the fetch requests takes longer than 30 seconds which results in an error. I don't really care about the fetch request result because it is a post request that is doing all it needs to however the error each time it runs is annoying.
Does anyone have any suggestions on how I can run the fetch request without waiting for a response? Or perhaps there is a way to handle the error gracefully?
Nov 26, 2024 09:49 PM
Hello @yiddy,
First of all can you please share your Automation Scripting block code. Then peoples can see it and give suggestion for optimization.
Also mention API documentation link that where do you post the data.
Mostly single reauest is works fine with that 30 second threshhold. So based on your request/response data I can give you more idea about it.
Another external option is use Make.com automation where it accept data as we webhook. You need to send data from airtable to Make using that same script then it will handle everything. Using of this method you don't need to care anything just need to send data to Make.
I hope this helps to you.
👍
Nov 28, 2024 01:38 AM
Hi,
To handle the fetch request without causing execution time issues or errors, you can try the following:
Use fetch in a non-blocking way (asynchronously):
You can initiate the fetch request asynchronously using fetch() but not wait for the response. Use the catch block to handle any errors gracefully.
fetch(url, { method: 'POST', body: data })
.then(response => {
// You can ignore the response if not needed
})
.catch(error => {
// Log error or handle gracefully without interrupting the rest of the script
});
2. Set timeout in the fetch options:
You can set a timeout for your fetch request to avoid it hanging indefinitely. This way, the request will fail gracefully if it exceeds the time limit.
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000); // 30 seconds timeout
fetch(url, {
method: 'POST',
body: data,
signal: controller.signal,
})
.then(response => { /* handle response */ })
.catch(error => {
if (error.name === 'AbortError') {
console.log('Request timed out');
} else {
console.log('Other error occurred');
}
})
.finally(() => clearTimeout(timeoutId));
3. Run it in the background:
If the result of the request isn’t necessary for the script’s functionality, consider running the request in a background worker or separate process. This keeps your main script running without waiting for the fetch response.
For more insights and solutions on automation and error handling, visit sincere mechanic, where we cover common scripting issues and troubleshooting tips.
Nov 28, 2024 02:04 AM - edited Nov 28, 2024 02:05 AM
Thank you. I am running my script in an automation so there is no error really from the fetch request however automation scripts automatically time out after 30 seconds. I could not get setTimeout to work in an automation script either.
Nov 28, 2024 02:53 AM
Hello @yiddy,
If the response and data log is not important then use 1) 2) methods suggestion from @georgehenry667.
2) Set timeout in the fetch options: This best if you know how to setup things.
Need more help then share your script.
👍