Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Mar 16, 2020 07:32 PM
Hey,
I’m a newbie with JavaScript so this might be obvious …
I need a delay(ms) method to slow down the script after 5 API calls (limit 5/min.). Problem is all the JavaScript help on the intertube points to the setTimeout() method of the Window object. But it appears that Window is not instantiated inside the Airtable code block, therefore, no setTimeout() method. What’s a good workaround for this?
What might I be missing - other methods?
Thanks
David
Mar 16, 2020 08:03 PM
This is the function I use:
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
then you can use like:
await timeout(5000)//waits 5 seconds
Mar 17, 2020 08:23 AM
hmmm…
@openside - why do I get an error when I try to use that in a Airtable block.
“Cannot find setTimeout()”
Mar 17, 2020 08:35 AM
I don’t believe the Scripting block has the limit on API calls that you would have if you were writing API code to be executed elsewhere, mainly because you’re not calling the API when using the Scripting block. The API is the means of accessing Airtable when you’re outside of the Airtable interface. The Scripting block is internal, so there are no such restrictions.
Mar 17, 2020 08:39 AM
Other way around @Justin_Barrett; I’m using a 3rd party API with a restriction. Alpha Vantage - for stock market prices.
Do you know why the setTimeout() is not found?
Mar 17, 2020 08:45 AM
Sorry. I thought you were referring to the Airtable API, not a third party API.
I’m just getting reacquainted with JavaScript myself, and haven’t yet hit a situation where I’d need a delay, so I’m not sure why setTimeout() isn’t available.
Mar 17, 2020 09:08 AM
@David_Koontz - their editor gives a warning, but the code runs fine. Their editor just doesn’t recognize it. You can tell it to ignore the error warning if you want by right clicking over it and clicking on the ‘Quick Fix’
Mar 17, 2020 09:41 AM
I’ve run up against what I believe to be a rate limit in the scripting block API. When I modified my script to slow calls down to about 5 requests per second, the script worked.
Mar 17, 2020 10:14 AM
I’m getting this Error:
ReferenceError: Can’t find variable: window
asyncFunctionResume@[native code] main anonymous s@blob:https://block--v-xd-mn-xix-b-lud-p5--06e4o42.airtableblocks.com/467c6f74-ac94-41bc-9fe7-45746e655847...
Any ideas why it works for you but not me?
Mar 17, 2020 10:17 AM
Strange, because I’ve got a script that does a ton of stuff to my base, and I haven’t encountered any timing-related issues that I know of. Again, this is just using block-native features to only read and modify Airtable records, not calls to external APIs.