Async/Await Not Working Properly with Airtable API in Node.js

I’m having trouble with async/await when fetching records from Airtable in my Node.js application. The await keyword seems to not wait for the promise to complete before moving to the next line.

Here’s my current code:

async function fetchRecord(identifier) {
  return await baseConnection("Records")
    .select({
      filterByFormula: `{identifier} = "${identifier}"`,
    })
    .firstPage((error, results) => {
      if (error) {
        console.log(error);
        return {};
      }

      console.log(results[0].fields);
      return results[0].fields;
    });
}
items.map(async (item) => {
      embedInfo = getEmbedInfo(item);

      record = await fetchRecord(embedInfo.identifier);

      console.log(record);
})

The output shows undefined values first, then the actual data appears later. It seems like the async function is not waiting for the Airtable query to finish. How can I fix this issue?

you got it! firstPage isn’t promise-based, so either wrap it in a promise or check for a promise method. also, switch the map to a for loop for async calls - this way, they’ll run in the correct order!