{"id":"d159ed4c30b915ea","repo":"colinhacks/zod","slug":"implementasync-must-be-called-with-a-function","errorCode":null,"errorMessage":"implementAsync() must be called with a function","messagePattern":"implementAsync\\(\\) must be called with a function","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/core/schemas.ts","lineNumber":4446,"sourceCode":"    inst._zod.def = def;\n\n    inst.implement = (func) => {\n      if (typeof func !== \"function\") {\n        throw new Error(\"implement() must be called with a function\");\n      }\n      return function (this: any, ...args: never[]) {\n        const parsedArgs = inst._def.input ? parse(inst._def.input, args) : args;\n        const result = Reflect.apply(func, this, parsedArgs as never[]);\n        if (inst._def.output) {\n          return parse(inst._def.output, result);\n        }\n        return result as any;\n      };\n    };\n\n    inst.implementAsync = (func) => {\n      if (typeof func !== \"function\") {\n        throw new Error(\"implementAsync() must be called with a function\");\n      }\n      return async function (this: any, ...args: never[]) {\n        const parsedArgs = inst._def.input ? await parseAsync(inst._def.input, args) : args;\n        const result = await Reflect.apply(func, this, parsedArgs as never[]);\n        if (inst._def.output) {\n          return await parseAsync(inst._def.output, result);\n        }\n        return result;\n      } as any;\n    };\n\n    inst._zod.parse = (payload, _ctx) => {\n      if (typeof payload.value !== \"function\") {\n        payload.issues.push({\n          code: \"invalid_type\",\n          expected: \"function\",\n          input: payload.value,\n          inst,","sourceCodeStart":4428,"sourceCodeEnd":4464,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/schemas.ts#L4428-L4464","documentation":"Thrown by `inst.implementAsync(func)` on a `z.function()` schema when the argument is not a function. `.implementAsync()` is the async variant of `.implement()`, returning an async function whose arguments/return are parsed with `parseAsync`; it still requires a function argument to wrap.","triggerScenarios":"Calling `.implementAsync(undefined)`, `.implementAsync(null)`, `.implementAsync('handler')`, or any non-function value. Same shape as the sync variant.","commonSituations":"Mistakenly passing a promise or a config object; handler variable is undefined due to a circular import or hoisting issue.","solutions":["Pass a function (sync or async) to `.implementAsync()`, e.g. `schema.implementAsync(async (arg) => ...)`.","Guard dynamically: `if (typeof handler === 'function') schema.implementAsync(handler)`.","Verify the handler is defined at the call site (check imports and initialization order)."],"exampleFix":"// before\nconst fn = z.function(z.string(), z.boolean()).implementAsync(undefined);\n\n// after\nconst fn = z.function(z.string(), z.boolean()).implementAsync(async (s) => !!s);","handlingStrategy":"type-guard","validationCode":"if (typeof handler !== 'function') throw new Error('handler must be a function');\nconst wrapped = fnSchema.implementAsync(handler);","typeGuard":"function isFunction(v): boolean { return typeof v === 'function'; }","tryCatchPattern":"try {\n  const wrapped = fnSchema.implementAsync(handler);\n} catch (e) {\n  if (e instanceof Error && e.message === 'implementAsync() must be called with a function') {\n    // confirm handler is a function and retry\n  }\n  throw e;\n}","preventionTips":["Type the handler as a function parameter to get compile-time safety.","Guard conditionally-defined async handlers with typeof checks.","Check that the handler import resolves (no circular-import undefined)."],"tags":["function-schema","async","api-misuse"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}