{"record":{"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":4463,"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":4445,"sourceCodeEnd":4481,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/schemas.ts#L4445-L4481","documentation":"Thrown by inst.implementAsync() on a $ZodFunction when the argument is not of type \"function\". implementAsync() is the async counterpart of implement(): it wraps a function whose argument/return parsing is awaited. The same typeof guard rejects non-callable values at the wrap site, before any promise machinery is set up.","triggerScenarios":"Calling z.function(...).implementAsync(null), .implementAsync({}), .implementAsync(123), or passing a non-function value. Like error 53 but for the async variant; also fires when a value intended to be an async handler is undefined (e.g. a missing import or a typo in the handler name).","commonSituations":"Using implementAsync where implement was intended but passing a non-function; passing a Promise (the result of calling the async function) instead of the function itself; refactors that leave the handler reference undefined; mocking in tests that substitutes a plain object for the handler.","solutions":["Pass an async function (or any function) to implementAsync().","Pass the function reference, not the result of calling it (no trailing parentheses).","Guard optional handlers with a conditional before calling implementAsync().","Verify imports resolve to actual functions in test setup."],"exampleFix":"// before\nconst fn = z.function(z.string(), z.string()).implementAsync(asyncHandler());\n// after\nconst fn = z.function(z.string(), z.string()).implementAsync(asyncHandler);","handlingStrategy":"type-guard","validationCode":"function implementAsyncOrThrow<F>(fnSchema: z.ZodFunction, fn: F) {\n  if (typeof fn !== \"function\") throw new Error(\"implementAsync() requires a function\");\n  return fnSchema.implementAsync(fn as any);\n}","typeGuard":"function isFunction(v: unknown): v is Function {\n  return typeof v === \"function\";\n}","tryCatchPattern":null,"preventionTips":["Pass the function reference, not its return value (no trailing parentheses).","Type the handler parameter as Function so tsc catches mistakes.","Check for undefined imports before wiring async handlers."],"tags":["function","implement-async","schema-construction","type-guard"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}