Async/Await function to loop through and POST to external URL - Works when running externally in node but cannot get it to work in Airtable scripts app

Hello! I have written a script which should be taking Airtable data (the actual use-case is URL redirects data) and posts to Shopify, however, I’m getting really stuck with this final element of my script!

I’m using the scripting app in Airtable. I’m fairly confident that this is an Airtable scripting app nuance because when I run this script in node with hardcoded Airtable data it all works as expected and posts my data to Shopify!

In the script below, I’m able to get the Airtable data as expected, and I’m also able to POST to Shopify outside of this function. However, I need this URL inside of a loop (forEach) so that I can POST each of the individual data sets to Shopify.

For reference, URLSettings() updates the body element of the JSON I am posting to Shopify. I am needing to loop in each item of the array and post one-by-one.

For reference, the console.log(result) below gives me an empty array.

const hardcodeArray = [

async function push() {
    let result = [];
    let arr = hardcodeArray;
    arr.forEach(async (element) => {
        let data = await remoteFetchAsync(shopifyURL, URLSettings(element)); 
        let postData = await data.json();

Hopefully this is the right place to post, but please do let me know if it should be somewhere else?!

Thanks in advance!!

Welcome to the community, @Vic_Graves! :smiley: This is the right place. :+1:

The .forEach() array method doesn’t work well with asynchronous functions. You’re giving the .forEach() method an asynchronous function to call, but it’s not actually being called asynchronously each time, which explains why you have an empty result array at the end. I suggest reformatting that part of the code using a standard for...of loop:

    for (let element of arr) {
        let data = await remoteFetchAsync(shopifyURL, URLSettings(element)); 
        let postData = await data.json();

Hi @Justin_Barrett thank you so much for responding and looking into this!! Still a beginner so really appreciate the help!!

I’ve tried updating with your code, and I’m still getting no response, which is confusing! Attached a screenshot of my actual code - you can see from my console.logs that the array is there before the for loop, but then line 100 logs only one element, which makes me think that the loop stops at line 101?

Ok, I’ve actually solved it! All I did was remove the wrapping async function and it works!!!

Glad to hear that you solved it! This is just a guess, but it might have failed because you weren’t calling that wrapping async function with the await keyword, so it might not have actually been running asynchronously. You might try putting it back and calling it with await push() instead of just push() and see if it works correctly.


Ahha!!! You are totally right!! Thanks @Justin_Barrett that actually worked also!!!

1 Like

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.