Deferred work
Discord waits only 3 seconds for the first response to an interaction. A handler that takes longer, such as one that calls another API, has to acknowledge the interaction first and send its reply later. Discord0 does that for you, and each adapter keeps the host running until the reply is sent.
Automatic deferral
Section titled “Automatic deferral”When a handler hasn’t responded after 2 seconds, the app sends a deferred response, and Discord shows “App is thinking…”. When the handler returns, its message replaces the loading state.
app.command("forecast", "Fetches the weather", async () => { const response = await fetch("https://api.example.com/forecast"); const { summary } = await response.json(); return summary; // Edits the deferred reply if this took over 2 seconds.});Two options control this:
const app = new Discord({ // When to defer, in milliseconds. Must be below 3000. Default: 2000, // or a lower host default from the adapter when you leave it unset. autoDeferAfterMs: 1500, // Whether an automatic deferral is ephemeral (visible only to the user // who ran the command). Default: true. autoDeferEphemeral: false,});A deferred reply keeps the visibility it was deferred with. With the default
autoDeferEphemeral: true, a slow handler’s reply is ephemeral even if it
returns a public message. Set it to false for public replies. With false, a
slow handler that returns an ephemeral message fails with EPHEMERAL_MISMATCH.
To keep that handler’s reply private, call c.defer(true) in it before the
slow work.
Per-host defaults
Section titled “Per-host defaults”autoDeferAfterMs is measured from when your handler starts, not from when
Discord sent the request. On serverless hosts, cold starts can use much of
Discord’s 3-second window before that. When you leave autoDeferAfterMs
unset, each hosted adapter applies its own default so the acknowledgement
still fits:
| Adapter | Default autoDeferAfterMs |
Why |
|---|---|---|
| Cloudflare Workers | 2000 | Cold starts are typically tens of milliseconds |
| Vercel Functions | 1000 | Cold starts often take 0.7 to 1.6 seconds before the handler runs |
| Netlify Functions | 500 | Cold starts can use most of the 3-second window |
| AWS Lambda | 1000 | Cold starts often take 0.7 to 1.5 seconds before the handler runs |
| Node.js / Bun | 2000 | A long-lived process has no cold start |
Setting autoDeferAfterMs in new Discord({ ... }) always wins over the
adapter default.
To defer yourself, call c.defer() before the slow work, or c.defer(true)
for an ephemeral reply. Then return the message or call c.reply(). After the
first response, you can also call c.editReply() and c.followUp().
How waitUntil keeps work alive
Section titled “How waitUntil keeps work alive”The app answers Discord’s HTTP request as soon as the interaction is
acknowledged, and the rest of the handler runs afterwards. On a serverless
host, a function can be frozen or stopped once it returns its response. So the
app passes the unfinished work to the host’s waitUntil, which keeps the
function alive until that work settles. Each adapter connects this for you:
| Adapter | How deferred work is kept alive |
|---|---|
| Cloudflare | ctx.waitUntil() from the Worker’s execution context |
| Vercel | waitUntil() from @vercel/functions |
| Netlify | context.waitUntil() from the function’s context |
| AWS | The handler streams the response, then waits for every pending promise before it returns |
| Node.js | The process keeps running, and graceful shutdown waits for pending work |
| Bun | The process keeps running, and graceful shutdown waits for pending work |
The app reports errors in deferred work to onError, and edits the reply to
the error message when it can.
Fallback messages and lifecycle hooks
Section titled “Fallback messages and lifecycle hooks”The app sends a fallback reply when a handler throws (error), when no
handler matches the interaction (unavailable), and when a handler returns
without replying (noResponse). Replace them to localize them or to add a
support link:
const app = new Discord({ messages: { error: "Something went wrong. https://example.com/support", unavailable: "This control is no longer available.", noResponse: "No reply was produced.", },});Lifecycle hooks add logging, tracing, or metrics without wrapping every
handler. beforeHandle runs before each verified interaction is routed.
afterHandle runs after routing finishes, with the duration in milliseconds.
onError receives handler errors and replaces the default error log:
app .beforeHandle((interaction) => { console.log("handling", interaction.id); }) .afterHandle((interaction, durationMs) => { console.log("done", interaction.id, durationMs); }) .onError((error, interaction, durationMs) => { console.error("failed", interaction.id, durationMs, error); });A hook that throws doesn’t stop the interaction. You can set hooks only on the main app. Mounting a sub-app that sets one throws.
If you call app.fetch from your own server code, pass your host’s
waitUntil as the second argument. Otherwise a serverless host can stop
deferred work once the response is sent:
app.fetch(request, { waitUntil: (promise) => keepAlive(promise) });Limits
Section titled “Limits”Two limits apply to every handler:
- Discord’s 15 minutes. Interaction tokens are valid for 15 minutes, so replies, edits, and follow-ups must be sent within 15 minutes of the interaction. (Discord docs)
- Your host’s limit, which is often shorter:
| Adapter | How long deferred work can run |
|---|---|
| Cloudflare Workers | 30 seconds after the response is sent, plus CPU time limits |
| Vercel Functions | Until the function’s maximum duration: 300 seconds by default, up to 800 on Pro and Enterprise |
| Netlify Functions | 60 seconds in total, including the response |
| AWS Lambda | Until the function timeout: 60 seconds in the generated config |
| Node.js | No host limit. On shutdown, your process manager’s stop timeout applies |
| Bun | No host limit. On shutdown, your process manager’s stop timeout applies |
The host cancels work that runs past its limit, and the user is left with the loading state. For longer jobs, acknowledge the interaction and hand the job to a queue or background worker on your host. Have that worker edit the reply through Discord’s webhook endpoints within 15 minutes.
Each adapter’s page links to the provider’s current documentation, since provider limits change.