{"record":{"id":"b8cd42a7d0c8504f","repo":"karatelabs/karate","slug":"promise-resolver-is-not-a-function","errorCode":null,"errorMessage":"Promise resolver is not a function","messagePattern":"Promise resolver is not a function","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsPromiseConstructor.java","lineNumber":90,"sourceCode":"        }\n        return scope;\n    }\n\n    static JsPromise newPromise(Context context) {\n        Engine engine = engineOf(context);\n        AsyncActivation.anyAsync = true;\n        return new JsPromise(engine, scopeOf(engine));\n    }\n\n    @Override\n    public Object call(Context context, Object[] args) {\n        CallInfo callInfo = context.getCallInfo();\n        if (callInfo == null || !callInfo.constructor) {\n            throw JsErrorException.typeError(\"Promise constructor cannot be invoked without 'new'\");\n        }\n        Object executor = AsyncSupport.arg(args, 0);\n        if (!(executor instanceof JsCallable callable)) {\n            throw JsErrorException.typeError(\"Promise resolver is not a function\");\n        }\n        JsPromise promise = newPromise(context);\n        AtomicBoolean claimed = new AtomicBoolean();\n        JsCallable resolveFn = (ctx, a) -> {\n            if (claimed.compareAndSet(false, true)) {\n                AsyncSupport.settleFromCallback(promise, AsyncSupport.arg(a, 0), false);\n            }\n            return Terms.UNDEFINED;\n        };\n        JsCallable rejectFn = (ctx, a) -> {\n            if (claimed.compareAndSet(false, true)) {\n                AsyncSupport.settleFromCallback(promise, AsyncSupport.arg(a, 0), true);\n            }\n            return Terms.UNDEFINED;\n        };\n        AsyncSupport.Completion outcome = AsyncSupport.invoke(\n                promise.engine, callable, new Object[]{resolveFn, rejectFn}, null);\n        if (outcome.threw() && claimed.compareAndSet(false, true)) {","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsPromiseConstructor.java#L72-L108","documentation":"`new Promise(executor)` requires the executor to be a function with `(resolve, reject)` parameters. When `AsyncSupport.arg(args, 0)` is anything other than a JsCallable (undefined, a value, a non-callable object), the engine throws this TypeError, matching the spec's IsCallable check.","triggerScenarios":"`new Promise()` with no argument; `new Promise(42)` or `new Promise({})`; passing a variable that was expected to be a function but is undefined due to a typo or earlier failure.","commonSituations":"Typos in callback names; forgetting to pass the executor during a refactor; dynamic executors loaded from data that turned out to be strings/values, not functions.","solutions":["Pass an executor function: `new Promise((resolve, reject) => ...)`.","Guard the argument before constructing: `if (typeof executor !== 'function') throw ...`.","Log or inspect the value if it should be a function — a failed definition above often leaves it undefined."],"exampleFix":"// before\nconst p = new Promise(config.executor); // executor is a plain object here\n// after\nif (typeof config.executor === 'function') {\n  const p = new Promise(config.executor);\n}","handlingStrategy":"validation","validationCode":"function safeNewPromise(executor) {\n  if (typeof executor !== 'function') throw new TypeError('Promise executor must be a function');\n  return new Promise(executor);\n}","typeGuard":"function isCallable(v) { return typeof v === 'function'; }","tryCatchPattern":"try { const p = new Promise(executor); } catch (e) { if (e instanceof TypeError && /resolver is not a function/.test(e.message)) executor = defaultExecutor; throw e; }","preventionTips":["Always inline the executor: `new Promise((resolve, reject) => ...)`.","Check upstream definitions when passing a function by reference.","Use an editor snippet/template for Promise construction."],"tags":["javascript","promise","executor","type-check"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}