Skip to main content
Solved

Async functions work in automations scripts but not scripting app?

  • Aug 11, 2021 12:14 PM UTC
  • 4 replies
  • 195 views

Forum|alt.badge.img+8

I’ve written an automation script that I’d like to migrate over to the scripting app. I know that there are some differences between the two, but I’m having a lot more trouble than I thought. The script connects to an external API and pulls data from the API into my base. I seem to be running into issues with the async functions I’ve written to connect to the API and pull the data in. The code works when it’s not wrapped up in a function, but fails when it looks like the example below:

async function testFunction(){
let response = await fetch('https://api.github.com/orgs/Airtable');
console.log(await response.json());
}
testFunction();

This code above works as an automation script, but not in the scripting app.

Best answer by Dominik_Bosnjak

Await the testFunction() invocation?

View original
This topic has been closed for replies.

4 replies

Dominik_Bosnjak
Forum|alt.badge.img+17

Await the testFunction() invocation?


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • Aug 13, 2021 4:55 PM UTC

You should await the testFunction, as Dominik mentioned.

Also, fetch works differently in automation scripts versus scripting app. Try using remoteFetchAsync instead of fetch. See the documentation for more details.


Forum|alt.badge.img+8
  • Author
  • Inspiring
  • Aug 14, 2021 11:35 AM UTC

Adding await solved it, thanks! Kuovonne, I can see why remoteFetchAsync might work better on an automation since it’s coming from the Airtable server itself. Thanks to you both for the help!


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • Aug 14, 2021 12:43 PM UTC

Adding await solved it, thanks! Kuovonne, I can see why remoteFetchAsync might work better on an automation since it’s coming from the Airtable server itself. Thanks to you both for the help!


Glad to hear your problem is fixed.

It’s actually the other way around. remoteFetchAsync is for use in scripting app, not in an automation script. It is most useful when fetch in scripting app causes CORS errors that don’t happen in automation scripts.