{"record":{"id":"2af342996c877ea3","repo":"caolan/async","slug":"arity-is-undefined","errorCode":null,"errorMessage":"arity is undefined","messagePattern":"arity is undefined","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/internal/awaitify.js","lineNumber":5,"sourceCode":"// conditionally promisify a function.\n// only return a promise if a callback is omitted\nexport default function awaitify (asyncFn, arity) {\n    if (!arity) arity = asyncFn.length;\n    if (!arity) throw new Error('arity is undefined')\n    function awaitable (...args) {\n        if (typeof args[arity - 1] === 'function') {\n            return asyncFn.apply(this, args)\n        }\n\n        return new Promise((resolve, reject) => {\n            args[arity - 1] = (err, ...cbArgs) => {\n                if (err) return reject(err)\n                resolve(cbArgs.length > 1 ? cbArgs : cbArgs[0])\n            }\n            asyncFn.apply(this, args)\n        })\n    }\n\n    return awaitable\n}\n","sourceCodeStart":1,"sourceCodeEnd":22,"githubUrl":"https://github.com/caolan/async/blob/13dfaf13f3fc809ba1c9c39d5e267ac6959cbf4a/lib/internal/awaitify.js#L1-L22","documentation":"awaitify wraps a callback-style function so it can return a promise when no callback is supplied. It derives the callback position from arity; if the passed arity argument is falsy AND asyncFn.length is 0, there is no way to know where the callback belongs, so it throws. This guards against wrapping functions that declare no parameters.","triggerScenarios":"Internally when a wrapped function has zero declared parameters (e.g. (...args) => {} rest-only functions have length 0) and no explicit arity is given, e.g. a task function written as function (...args) {} passed to a promisified API.","commonSituations":"Using rest-parameter task functions with async promisification, minified functions compiled to rest args, passing a wrapper around the real callback-taking function.","solutions":["Declare the callback as a named parameter so asyncFn.length is > 0: function (callback) {...}","Wrap the function to expose correct arity: const wrapped = (cb) => original(cb)","Upgrade async (newer versions handle rest-arg functions better)","Call the callback-taking function directly instead of via promisification"],"exampleFix":"// before\nconst fn = (...args) => legacy(...args); // length 0\n// after\nconst fn = (callback) => legacy(callback); // length 1","handlingStrategy":"type-guard","validationCode":"const isPromisifiable = (fn) => typeof fn === 'function' && fn.length > 0;","typeGuard":"function hasCallbackArity(fn) {\n  return typeof fn === 'function' && fn.length > 0;\n}","tryCatchPattern":"try {\n  const promisified = awaitify(fn);\n} catch (err) {\n  if (String(err.message) === 'arity is undefined') {\n    console.error('Function must declare a callback parameter');\n  } else throw err;\n}","preventionTips":["Avoid rest-only (...args) signatures for functions you promisify","Declare the callback as the last named parameter","Prefer async.util promisification on explicitly-shaped functions"],"tags":["async","promisify","arity"],"backgroundTag":"invalid-arity","analyzedSha":"13dfaf13f3fc809ba1c9c39d5e267ac6959cbf4a","analyzedAt":"2026-08-28T22:50:46.507Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}