StoicBuilder

HandbookI the LudusAsync and promises

Ithe Ludus, Read code

Async and promises

1 min readUpdated 28 November

In plain words

Async code starts a slow task, like loading rows from Supabase, and gets back a promise, an IOU that await turns into the real result.

Where you see it

Any line with await or .then(). When your Chirp feed flashes "No posts yet" for half a second before the posts appear, that gap is a promise waiting on the database.

src/lib/posts.tsbrowser
export async function loadPosts() { const { data, error } = await supabase .from("posts") .select("*"); if (error) throw error; return data;}
await pauses this function, not the page, until Supabase answers.

What goes wrong

  1. Forgetting await

    Without it you hold the IOU, not the data. posts.map then fails with "posts.map is not a function", because a promise has no map.

    const posts = loadPosts(); // a Promise, not posts
  2. Showing "empty" while still loading

    Until the promise settles there's no data yet. Without a separate loading state, users see "No posts yet" before the real posts arrive.

  3. Waiting in a queue for no reason

    Three awaits in a row wait three times. Requests that don't depend on each other can run together with Promise.all and finish as fast as the slowest one.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Make sure every call to Supabase or fetch in this file is awaited and handles the error case. Give the page separate loading, error and empty states, and point out any requests that could run in parallel.

Quick check

Sign in to earn +10 XP for it

The code says const posts = loadPosts(); and the page crashes with "posts.map is not a function". What's missing?

Got a prompt that touches async and promises?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.