Why does this not run in an async function?

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
        })
    }
}

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.

1 Like

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

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