{"record":{"id":"607c15ebe7ae6ae6","repo":"petkaantonov/bluebird","slug":"expecting-a-function-but-got-s","errorCode":null,"errorMessage":"expecting a function but got %s","messagePattern":"expecting a function but got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/generators.js","lineNumber":213,"sourceCode":"        throw new TypeError(NOT_GENERATOR_ERROR);\n    }\n    var yieldHandler = Object(options).yieldHandler;\n    var PromiseSpawn$ = PromiseSpawn;\n    var stack = new Error().stack;\n    return function () {\n        var generator = generatorFunction.apply(this, arguments);\n        var spawn = new PromiseSpawn$(undefined, undefined, yieldHandler,\n                                      stack);\n        var ret = spawn.promise();\n        spawn._generator = generator;\n        spawn._promiseFulfilled(undefined);\n        return ret;\n    };\n};\n\nPromise.coroutine.addYieldHandler = function(fn) {\n    if (typeof fn !== \"function\") {\n        throw new TypeError(FUNCTION_ERROR + util.classString(fn));\n    }\n    yieldHandlers.push(fn);\n};\n\nPromise.spawn = function (generatorFunction) {\n    debug.deprecated(\"Promise.spawn()\", \"Promise.coroutine()\");\n    //Return rejected promise because Promise.spawn is semantically\n    //something that will be called at runtime with possibly dynamic values\n    if (typeof generatorFunction !== \"function\") {\n        return apiRejection(NOT_GENERATOR_ERROR);\n    }\n    var spawn = new PromiseSpawn(generatorFunction, this);\n    var ret = spawn.promise();\n    spawn._run(Promise.spawn);\n    return ret;\n};\n};\n","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/petkaantonov/bluebird/blob/c220cfe480868e2f5c5de4fe8bb3b04b97588d7f/src/generators.js#L195-L231","documentation":"Promise.coroutine.addYieldHandler(fn) registers a handler deciding what happens when a non-promise value is yielded inside a coroutine. It requires fn to be a function and throws TypeError('expecting a function but got %s') otherwise.","triggerScenarios":"Promise.coroutine.addYieldHandler(undefined/null/object) — typically passing a variable that is undefined due to a bad import, forgetting the callback argument, or passing the handled value instead of a handler function.","commonSituations":"Misconfigured imports in refactorings; conditional code paths where the handler variable was never assigned; copying examples and leaving a placeholder argument; passing a class instead of an instance method bound function.","solutions":["Pass an actual function: Promise.coroutine.addYieldHandler(value => handle(value))","Verify the identifier is a defined function before registering (typeof fn === 'function')","Register the yield handler once at startup before creating coroutines","Check for typos or arguments accidentally omitted in the call"],"exampleFix":"// before\nPromise.coroutine.addYieldHandler(thunk); // thunk undefined\n// after\nPromise.coroutine.addYieldHandler(value =>\n  value && typeof value.then === 'function' ? value : Promise.resolve(value)\n);","handlingStrategy":"type-guard","validationCode":"const handler = value => Promise.resolve(value);\nif (typeof handler !== 'function') throw new TypeError('yield handler must be a function');\nPromise.coroutine.addYieldHandler(handler);","typeGuard":"function isValidYieldHandler(fn) {\n  return typeof fn === 'function' && fn.length <= 1;\n}","tryCatchPattern":"try {\n  Promise.coroutine.addYieldHandler(myHandler);\n} catch (e) {\n  if (String(e.message).startsWith('expecting a function')) {\n    console.error('myHandler is not a function:', typeof myHandler);\n  } else throw e;\n}","preventionTips":["Register yield handlers at startup with a literal function argument","Check typeof before passing dynamically selected handlers","Document that addYieldHandler takes a function receiving the yielded value","Avoid conditional registration paths where the handler may be undefined"],"tags":["typeerror","yieldhandler","coroutine"],"backgroundTag":"expecting-a-function","analyzedSha":"c220cfe480868e2f5c5de4fe8bb3b04b97588d7f","analyzedAt":"2026-09-02T02:30:20.863Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}