I need help with async loops in Next.js when working with multiple Airtable databases inside getStaticProps.
I want to iterate through an array of database identifiers like ["appDb001", "appDb002", "appDb003"] and fetch data from each one. Currently my setup works fine with a single database but I’m stuck on handling multiple ones.
I hit this exact problem a few months ago with multiple Airtable bases for a client dashboard. You’ve got two options: parallel vs sequential execution.
Parallel is faster but hammers the rate limits harder. Use Promise.all:
This saved me tons of time since all requests fire at once. Just watch Airtable’s API limits if you’re dealing with many bases.
I stick with Promise.all unless I’m hitting rate limits. Then I switch to sequential like Jessica mentioned. Parallel typically cuts my build times in half vs the for-of loop.
Use Promise.all with map instead of manual calls. Try const results = await Promise.all(dbIds.map(async (id) => { const data = await getDataBase(id).select().firstPage(); return processRecords(data); })); - this fetches all DBs at once instead of one by one, so it’s way faster.
Been working with Next.js and Airtable for over a year now and honestly the Promise.allSettled approach saved me countless headaches. Promise.all fails completely if any database throws an error, but Promise.allSettled lets you handle partial failures gracefully.
If one database is temporarily down or has issues, your build won’t completely fail. You get data from working databases and can log the failures. Way more robust than other solutions when dealing with external APIs that might have occasional hiccups.
Your manual approach works, but you can make it dynamic with a simple for-of loop. I’ve dealt with similar situations and this is way more readable than Promise.all when you need sequential processing:
This processes databases one at a time, which helps with Airtable’s rate limits. If you don’t care about rate limits and want everything to run in parallel, Promise.all works great too.