Here's a pattern that's probably in your codebase:

export async function POST(request: Request) { const result = await doTheWork(request); // Don't make the user wait for bookkeeping. void recordAudit({ userId, ok: true }); void recordCost({ workspaceId, tokens: result.tokens }); return Response.json(result); } 

It reads as "start these writes, don't wait for them". On a long-running Node server, that's what happens. On a serverless platform, it's a hope.

What we measured

We noticed our AI cost ledger had no rows for days on end, even though AI calls were clearly happening. So we ran two identical requests against production from a fresh test account and watched the database:

  • The first request's audit row arrived about 18 seconds after its response, presumably when the function instance was woken up again.
  • The second request's audit row never arrived.
  • Neither request wrote its cost-ledger row.

Nothing errored and nothing was logged. The writes just didn't happen, or happened whenever the instance next woke up.

Why

Once a serverless function returns its response, the platform is free to freeze the instance or recycle it. A promise you didn't await is just pending work inside a process that has stopped running. If the instance gets reused, the work resumes late. If it doesn't, the work is gone.

The platform primitive for "keep this invocation alive until this promise settles" is waitUntil. Next.js exposes it as after(), stable since 15.1:

import { after } from "next/server"; after(recordAudit({ userId, ok: true })); // a promise works as well as a callback 

The Next.js docs say it directly: on Vercel, after is implemented with waitUntil(promise), which extends the life of the invocation until every promise passed to it has settled.

The fix: one helper, applied in six places

We had about 30 void someWrite(...) call sites. Rather than touch each one, we fixed the functions they call: the email sender, the Telegram alert sender, the audit writer, the cost-ledger writer, and the webhook and integration dispatchers.

import { after } from "next/server"; /** * Keep the invocation alive until `work` settles, without making the caller wait. * Returns `work` unchanged, so `await keepAlive(p)` and `void keepAlive(p)` behave * exactly like `await p` and `void p`. */ export function keepAlive<T>(work: Promise<T>): Promise<T> { try { after(work); } catch { // Outside a request scope (tests, scripts) `after` throws. The work is // already running, so let it finish the way it always did. } return work; } export function recordAudit(input: AuditInput): Promise<void> { return keepAlive(insertAudit(input)); } 

Three things make this safe to drop in:

  1. It returns the same promise. Callers that await still await, and callers that void still don't wait.
  2. It's registered synchronously. Each wrapped function starts its work and calls keepAlive before its first await, so registration happens while the request scope is definitely still open.
  3. It degrades to the old behaviour. In a unit test or a script there's no request scope, so after throws. The helper swallows that and the work runs as it did before.

After deploying, we reran the same production probe. The audit row landed straight away.

A test gotcha we hit on the way

Our first test for the helper failed with the exact error we were trying to swallow:

const afterMock = vi.fn(); beforeEach(() => afterMock.mockReset()); // ← the bug 

Vitest treats a function returned from beforeEach as a cleanup hook. mockReset() returns the mock itself, so after each test Vitest called our throwing mock as teardown. A block body fixes it:

beforeEach(() => { afterMock.mockReset(); }); 

Checklist

  • Search for void followed by a call, and for .then( chains nobody awaits, in route handlers and server actions.
  • Fix them in the shared function, not at the call sites. Every future caller is then covered too.
  • Pay particular attention to anything a customer would notice going missing: welcome emails, receipts, webhooks.
  • Verify against production, not just locally. On next dev everything stays alive, so this bug can't show up there.

We found this in DodoForm, an AI form builder, while tracking down why its cost ledger was empty. The probe script we used is a plain Node script that signs in as a throwaway account, makes one request and polls the database for the row.