Help

Re: Why does this not run in an async function?

Solved
Jump to Solution
723 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Janene_Pappas
5 - Automation Enthusiast
5 - Automation Enthusiast

I have the following code that runs fine if I put it in the main body of my App but not if I put it in a function. I’m baffled, hoping folks here can enlighten me as to why. When in the main body, it runs through all of the records in the Teams table. When in a function, only the 1st record is updated. The portion I’d like to have in a function is the code inside of the if (allUsers) block. I have other functions that update in loops, what is so special about this one?

let teamsTable = base.getTable("Teams");
let teamsRecords = await teamsTable.selectRecordsAsync({
    sorts: [{field:"active members", direction: 'asc'}],
    fields: ["active members", "Included Players"]
}); 

if (allUsers) {
    console.log("rebalancing");

    let lowestActiveCount = teamsRecords.records[0].getCellValue("active members");
    console.log("Lowest active count: " + lowestActiveCount);
    let included = lowestActiveCount - 5;

    for (let team of teamsRecords.records) {
        console.log("Changing Included to: " + included);
        await teamsTable.updateRecordAsync(team.id, {
            "Included Players": included
        })
    }
}
1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

Welcome to the Airtable community!

When you put it in a function, make sure that (1) you declare the function as an async function, and (2) you use the await keyword when you call the function.

See Solution in Thread

2 Replies 2
kuovonne
18 - Pluto
18 - Pluto

Welcome to the Airtable community!

When you put it in a function, make sure that (1) you declare the function as an async function, and (2) you use the await keyword when you call the function.

Ahhhh I’d missed the “await” on the function call. Thank you!