{"id":"3007249c7987f977","repo":"sindresorhus/is","slug":"expected-value-which-is-asyncfunction-received","errorCode":null,"errorMessage":"Expected value which is `AsyncFunction`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `AsyncFunction`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1462,"sourceCode":"\t}\n}\n\nexport function assertArrayBuffer(value: unknown, message?: string): asserts value is ArrayBuffer {\n\tif (!isArrayBuffer(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('ArrayBuffer', value));\n\t}\n}\n\nexport function assertArrayLike<T = unknown>(value: unknown, message?: string): asserts value is ArrayLike<T> {\n\tif (!isArrayLike(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('array-like', value));\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\nexport function assertAsyncFunction(value: unknown, message?: string): asserts value is Function {\n\tif (!isAsyncFunction(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('AsyncFunction', value));\n\t}\n}\n\nexport function assertAsyncGenerator(value: unknown, message?: string): asserts value is AsyncGenerator {\n\tif (!isAsyncGenerator(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('AsyncGenerator', value));\n\t}\n}\n\nexport function assertAsyncGeneratorFunction(value: unknown, message?: string): asserts value is AsyncGeneratorFunction {\n\tif (!isAsyncGeneratorFunction(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('AsyncGeneratorFunction', value));\n\t}\n}\n\nexport function assertAsyncIterable<T = unknown>(value: unknown, message?: string): asserts value is AsyncIterable<T> {\n\tif (!isAsyncIterable(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('AsyncIterable', value));","sourceCodeStart":1444,"sourceCodeEnd":1480,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1444-L1480","documentation":"Thrown by `assertAsyncFunction(value)` when the value's internal object tag is not `AsyncFunction` — i.e. it was not declared with `async function`/`async () =>`. Crucially, an ordinary function that returns a Promise is NOT an AsyncFunction by this check, since the detection is syntactic (via the object tag), not behavioral.","triggerScenarios":"Passing a regular function that returns a Promise (`() => Promise.resolve(x)`), a `.bind()`-wrapped async function in some transpiled environments, a promisified callback function, or a transpiled-to-ES5 async function (Babel/TypeScript targeting older ES downlevels async to a generator/plain function, losing the AsyncFunction tag).","commonSituations":"Build tooling with `target: es5`/`es2015` in tsconfig or Babel stripping the native async tag, so `assert.asyncFunction` fails in production builds but passes in dev; libraries accepting handlers where users pass promise-returning plain functions; mocks/spies (jest.fn wrapping) that aren't tagged AsyncFunction.","solutions":["Declare the callback with the `async` keyword instead of returning a Promise manually.","If promise-returning plain functions should be accepted, assert `assert.function` and check the returned value with `assert.promise` at call time instead.","Raise your compile target to ES2017+ so async functions are emitted natively and keep their tag.","Unwrap mocking-framework wrappers or make the mock implementation itself an async function."],"exampleFix":"// before\nregisterHandler(() => fetchData()); // plain fn returning Promise, throws\n// after\nregisterHandler(async () => fetchData());","handlingStrategy":"type-guard","validationCode":"const AsyncFunction = (async () => {}).constructor;\nif (!(fn instanceof AsyncFunction)) { /* handle */ }","typeGuard":"const isAsyncFunction = (fn) =>\n  typeof fn === 'function' && fn.constructor?.name === 'AsyncFunction';","tryCatchPattern":"try {\n  assert.asyncFunction(fn);\n} catch (error) {\n  if (error instanceof TypeError) { /* fn is sync or not a function */ }\n  else { throw error; }\n}","preventionTips":["A sync function returning a Promise is NOT an AsyncFunction — only functions declared with async pass","Transpilers (Babel/TS targeting ES5) rewrite async functions into plain functions; check your build target","Prefer accepting any function and awaiting its result (is.function_ + Promise.resolve) unless you truly need the async tag"],"tags":["type-assertion","async","transpilation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}