{"record":{"id":"6821bf4ed08ac4f8","repo":"petkaantonov/bluebird","slug":"expecting-a-function-but-got-s-6821bf","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/promise.js","lineNumber":90,"sourceCode":"var Context = require(\"./context\")(Promise);\n /*jshint unused:false*/\nvar createContext = Context.create;\n\nvar debug = require(\"./debuggability\")(Promise, Context,\n    enableAsyncHooks, disableAsyncHooks);\nvar CapturedTrace = debug.CapturedTrace;\nvar PassThroughHandlerContext =\n    require(\"./finally\")(Promise, tryConvertToPromise, NEXT_FILTER);\nvar catchFilter = require(\"./catch_filter\")(NEXT_FILTER);\nvar nodebackForPromise = require(\"./nodeback\");\nvar errorObj = util.errorObj;\nvar tryCatch = util.tryCatch;\nfunction check(self, executor) {\n    if (self == null || self.constructor !== Promise) {\n        throw new TypeError(CONSTRUCT_ERROR_INVOCATION);\n    }\n    if (typeof executor !== \"function\") {\n        throw new TypeError(FUNCTION_ERROR + util.classString(executor));\n    }\n\n}\n\nfunction Promise(executor) {\n    if (executor !== INTERNAL) {\n        check(this, executor);\n    }\n    this._bitField = NO_STATE;\n    this._fulfillmentHandler0 = undefined;\n    this._rejectionHandler0 = undefined;\n    this._promise0 = undefined;\n    this._receiver0 = undefined;\n    this._resolveFromExecutor(executor);\n    this._promiseCreated();\n    this._fireEvent(\"promiseCreated\", this);\n}\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/petkaantonov/bluebird/blob/c220cfe480868e2f5c5de4fe8bb3b04b97588d7f/src/promise.js#L72-L108","documentation":"Thrown by the internal `check` function (src/promise.js:90) when the Promise constructor is invoked incorrectly — either called without `new`, on a subclass whose constructor identity changed, or when the executor argument is not a function. Bluebird guards its constructor so the API cannot be misused silently.","triggerScenarios":"Calling `Promise(resolver)` without `new`; `const P = Promise; P(fn)` is fine, but calling a rebound/stolen constructor (e.g. `Promise.call(obj, fn)`) fails the `self.constructor !== Promise` check; `new Promise(nonFunction)` such as `new Promise(undefined)`.","commonSituations":"Forgetting `new` after refactoring; subclassing Promise and overriding constructor; passing a thenable or object instead of a resolver function; transpilers or wrappers that re-invoke the constructor.","solutions":["Always call the constructor with `new`: `new Promise((resolve, reject) => ...)`.","Pass a function as the executor; if you have a promise already, don't wrap it — use it directly or `Promise.resolve(x)`.","If subclassing, ensure the subclass properly calls `super(executor)` and doesn't break the constructor identity.","If you need a promise from a non-function value, use `Promise.resolve(value)` instead of the constructor."],"exampleFix":"// before\nconst p = Promise((resolve) => resolve(1));\n// after\nconst p = new Promise((resolve) => resolve(1));","handlingStrategy":"type-guard","validationCode":"function safeNewPromise(executor) {\n  if (typeof executor !== 'function') {\n    throw new TypeError('executor must be a function, got ' + typeof executor);\n  }\n  return new Promise(executor);\n}","typeGuard":"const isExecutor = (v) => typeof v === 'function';","tryCatchPattern":"let p;\ntry { p = new Promise(executor); } catch (e) {\n  if (e instanceof TypeError) { /* fall back to Promise.resolve(value) */ p = Promise.resolve(null); }\n  else throw e;\n}","preventionTips":["Always use `new Promise((resolve, reject) => ...)`.","Never call the Promise constructor as a plain function or with a rebound this.","Don't wrap values that are already promises; use Promise.resolve."],"tags":["typeerror","constructor","promise"],"backgroundTag":"promise-constructor-misuse","analyzedSha":"c220cfe480868e2f5c5de4fe8bb3b04b97588d7f","analyzedAt":"2026-09-02T02:30:20.863Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}