A Zod 4 webhook schema gains an async refinement that looks up the event ID, and safeParse now throws 'Encountered Promise during synchronous parse'. Why, and what fixes it?
answer
- a call that cannot wait
- not a validation failure
- which error class is thrown
- the async twin of each call
basics
~20 sZod's synchronous parse and safeParse cannot await, so when a refinement returns a Promise they throw a plain Error instead of reporting an issue. Parse that schema, and every schema embedding it, with safeParseAsync or parseAsync.
solid answer
~40 sAn async refinement returns a Promise, and Zod 4's synchronous `parse` and `safeParse` cannot wait for one, so Zod throws `Encountered Promise during synchronous parse. Use .parseAsync() instead.` That is a plain `Error`, not a `ZodError`: `safeParse` does not turn it into `success: false`, and a handler that checks `instanceof z.ZodError` treats it as a server failure. It slipped through because Zod skips a value's checks once it has failed its base type, so tests that fed bad input never reached the refinement. The fix is `await schema.safeParseAsync(input)` for this schema and every schema that embeds it, plus a test with valid input. I would also ask whether the lookup belongs in the schema or after parsing.
code
typescript · 26 linesimport * as z from "zod";
const seen = new Set<string>(["evt_1"]);
const isNew = async (id: string) => !seen.has(id);
const WebhookEvent = z.object({
id: z.string().refine(isNew, { error: "duplicate event" }),
});
async function main() {
try {
WebhookEvent.safeParse({ id: "evt_2" });
} catch (err) {
console.log(err instanceof z.ZodError); // false
console.log((err as Error).message);
// Encountered Promise during synchronous parse. Use .parseAsync() instead.
}
const bad = WebhookEvent.safeParse({ id: 42 });
console.log(bad.success); // false: the refinement never ran
const dup = await WebhookEvent.safeParseAsync({ id: "evt_1" });
console.log(dup.success); // false: duplicate event
}
main();go deeper
Remember that a schema with any async refinement or transform must be parsed with parseAsync or safeParseAsync, and that the result needs await.
Explain that the synchronous calls cannot wait, so Zod throws a plain Error rather than adding an issue, and that safeParse does not convert it into a failure result.
Diagnose why it reached production: skipped checks after a type failure hid it from bad-input tests, and an instanceof ZodError handler mislabelled it as a server error.
Weigh whether I/O belongs in schemas at all: async refinements spread async calls to every consumer, while a post-parse lookup keeps schemas pure at the cost of separate error handling.
## What the message means Zod 4 runs a schema in one of two modes. The synchronous calls, `parse` and `safeParse`, walk the schema and expect every step to finish immediately. The async calls, `parseAsync` and `safeParseAsync`, are allowed to wait. When a **refinement** (a custom check added with `.refine`) or a **transform** returns a Promise during a synchronous call, Zod has no way to wait for it, so it stops and throws: ```plaintext Encountered Promise during synchronous parse. Use .parseAsync() instead. ``` The thrown object is a plain `Error` (the class is `z.core.$ZodAsyncError`), **not a `ZodError`**. It is not a validation result at all. It is Zod telling you the call does not match the schema. Writing async refinements and transforms is covered in the lesson on refinements and transforms; this question is about which parse call such a schema forces, and why the failure is easy to ship. ## Why safeParse does not protect you Teams reach for `safeParse` because it "does not throw". That promise covers **invalid input** only. The async mismatch is thrown, not returned: - `safeParse` does **not** turn it into `{ success: false }`. The exception escapes the call. - A handler that catches errors and checks `err instanceof z.ZodError` to decide "bad input, return a client error" will **not** recognise it, so it surfaces as a server failure. - By the time Zod throws, it has **already called your refinement**, so the lookup it wraps has been started and its result is discarded. ## Why the tests did not catch it The failure depends on the input reaching the refinement: 1. By default, Zod skips a schema's checks once the value has already failed its base type. An `id` that is a number instead of a string fails as `invalid_type`, and the async refinement attached to it never runs, so no Promise ever appears. 2. So a test suite that mostly feeds the schema **bad input** gets ordinary `success: false` results and passes. 3. The first input that is valid enough to reach the refinement, usually a real event from the provider, triggers the throw. The same applies when the async step sits deep inside a nested schema: **async is contagious**. Any schema that embeds an async piece must be parsed with an async call, all the way up to the boundary. ## The fix 1. Switch every call on that schema, and on every schema that embeds it, to `await schema.safeParseAsync(input)` or `await schema.parseAsync(input)`. (`.spa()` is an alias of `safeParseAsync`.) 2. Make the caller `async` and `await` the result; a forgotten `await` gives you a Promise where a result was expected. 3. Keep the `instanceof z.ZodError` check for `parseAsync`, and the `success` check for `safeParseAsync`; the result shapes are identical to the synchronous versions. 4. Add a test with **valid** input that reaches every refinement, so a future synchronous call fails in CI instead of in production. ## Should the lookup live in the schema at all? An async refinement makes every parse touch the store it queries, including in places that only wanted a shape check. A common alternative for a duplicate-delivery check is to keep the schema synchronous and run the lookup **after** parsing, on the typed `id`. That keeps `safeParse` usable, keeps parsing free of I/O and makes the side effect explicit. The trade-off is that the duplicate check no longer appears as a Zod issue, so the error response has to be built separately. | Approach | Parse call | Duplicate shows up as | Cost | |---|---|---|---| | Async refinement in the schema | `safeParseAsync` everywhere | a Zod issue on `id` | every parse queries the store | | Sync schema, lookup after parse | `safeParse` | your own error | lookup is separate code to maintain | ## Points an interviewer looks for - The cause: a Promise inside a synchronous parse, not bad input. - That `safeParse` throws here and the error is not a `ZodError`. - That checks on a value that already failed its type are skipped, which is why bad-input tests pass. - That every schema embedding the async piece needs an async call. - A deliberate choice about whether I/O belongs in the schema.
- In Zod 4, the async refinement lives in an address schema nested three levels inside an order schema. Which parse call does the order schema need?An async one: `parseAsync` or `safeParseAsync` on the order schema. The nested refinement still returns a Promise when the outer schema walks into it, so a synchronous call on the outer schema throws the same error. Async is contagious upward, which is why a reused sub-schema gaining an async check can break call sites far from where it changed.
- In Zod 4, why might a team keep a duplicate-event lookup out of the schema even though an async refinement would work?An async refinement makes every parse of that schema perform I/O and forces async calls wherever the schema is used, including tests and places that only need a shape check. Running the lookup after `safeParse` on the typed `id` keeps parsing pure and synchronous. The cost is that the duplicate no longer appears as a Zod issue, so its error response is built separately.
- safeParse catches every error, so it can never throw
- The thrown error is a ZodError, so the usual instanceof check handles it
- Only the schema that declares the async refinement needs an async call
- Tests with invalid input prove the async refinement works
- Zod waits for the Promise inside a synchronous parse but logs a warning