{"record":{"id":"f550413e61fb7749","repo":"karatelabs/karate","slug":"promise-constructor-cannot-be-invoked-without-new","errorCode":null,"errorMessage":"Promise constructor cannot be invoked without 'new'","messagePattern":"Promise constructor cannot be invoked without 'new'","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsPromiseConstructor.java","lineNumber":86,"sourceCode":"    private static AsyncScope scopeOf(Engine engine) {\n        AsyncScope scope = engine.currentScope();\n        if (scope == null) {\n            throw JsErrorException.typeError(\"Promise requires an active evaluation\");\n        }\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;","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsPromiseConstructor.java#L68-L104","documentation":"Per the ES spec, Promise is a constructor and must be called with `new`. Karate checks the call's CallInfo flag `constructor`; a bare `Promise(...)` call reaches `call()` without constructor mode and throws this TypeError. This mirrors browsers/V8 exactly (`TypeError: Promise resolver X is not a constructor`-style rejection of missing new).","triggerScenarios":"Writing `Promise(...)` or `Promise.call(...)` instead of `new Promise(...)`; forwarding Promise as a plain function reference, e.g. `const P = Promise; P(executor)`.","commonSituations":"Refactored code where `new` was dropped; destructured/aliased Promise; code translated from languages where object construction doesn't use `new`.","solutions":["Add the `new` keyword: `new Promise((resolve, reject) => {...})`.","If wrapping, bind through a helper that constructs: `const make = (ex) => new Promise(ex);`","Search the script for direct `Promise(` call sites (no preceding new)."],"exampleFix":"// before\nconst p = Promise((resolve) => resolve(1));\n// after\nconst p = new Promise((resolve) => resolve(1));","handlingStrategy":"type-guard","validationCode":"if (typeof Promise !== 'function') throw new Error('Promise unavailable');\n// always construct:\nif (typeof executor !== 'function') throw new TypeError('executor must be a function');","typeGuard":"function isPromiseCtor(f) { return f === Promise; }","tryCatchPattern":"try { const p = new Promise(executor); } catch (e) { if (e instanceof TypeError) { /* missing 'new' or bad executor */ } throw e; }","preventionTips":["Lint rule: flag bare `Promise(` calls without `new`.","Never alias Promise for direct invocation; alias a factory instead.","Code review Promise usage after refactors of async code."],"tags":["javascript","promise","constructor"],"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"}