{"record":{"id":"d1264554af1eea1b","repo":"karatelabs/karate","slug":"yield-is-only-valid-inside-a-generator-function","errorCode":null,"errorMessage":"yield is only valid inside a generator function","messagePattern":"yield is only valid inside a generator function","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Interpreter.java","lineNumber":2880,"sourceCode":"            return value;\n        }\n        try {\n            return AsyncSupport.await(value, context);\n        } catch (AsyncSupport.AwaitRejection e) {\n            context.stopAndThrow(e.reason);\n            return Terms.UNDEFINED;\n        }\n    }\n\n    /**\n     * {@code yield x} / {@code yield* iterable}. Node shape:\n     * [yield-token, STAR?, EXPR?]. Runs only on a generator's vthread —\n     * {@link GeneratorActivation#current()} is the brand.\n     */\n    private static Object evalYieldExpr(Node node, CoreContext context) {\n        GeneratorActivation act = GeneratorActivation.current();\n        if (act == null) {\n            throw JsErrorException.syntaxError(\"yield is only valid inside a generator function\");\n        }\n        boolean star = node.size() > 1 && node.get(1).isToken()\n                && node.get(1).token.type == TokenType.STAR;\n        Node operandNode = null;\n        if (star) {\n            operandNode = node.get(2);\n        } else if (node.size() > 1) {\n            operandNode = node.get(1);\n        }\n        Object operand = operandNode == null ? Terms.UNDEFINED : eval(operandNode, context);\n        if (context.isStopped()) {\n            return operand;\n        }\n        if (star) {\n            return evalYieldStar(operand, context, act);\n        }\n        return applyResume(act.yieldAndReceive(operand), context);\n    }","sourceCodeStart":2862,"sourceCodeEnd":2898,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Interpreter.java#L2862-L2898","documentation":"Thrown as a syntax error when `yield` (or `yield*`) is evaluated while no GeneratorActivation is current. `yield` is only meaningful inside a generator function's executing frame; outside one the engine cannot suspend and resume the evaluation.","triggerScenarios":"Using `yield` in a plain function, in top-level script, or calling the generator's inner function object directly rather than through the generator protocol (next()).","commonSituations":"Copy-pasting generator bodies into regular functions; forgetting `function*` syntax when declaring the generator; invoking yield-containing code from a callback.","solutions":["Declare the containing function as a generator: `function* gen() { yield x; }`.","Consume the generator via `it.next()` / iteration instead of calling it directly.","Replace `yield` with a plain return if no suspension/resume semantics are needed."],"exampleFix":"// before\nfunction produce() { yield 1; }\n// after\nfunction* produce() { yield 1; }\nvar it = produce();","handlingStrategy":"validation","validationCode":"if (!/^function\\s*\\*/.test(String(fn))) throw new Error('yield requires a generator function');","typeGuard":"function isGeneratorFn(f) { return typeof f === 'function' && /^\\s*function\\s*\\*/.test(String(f)); }","tryCatchPattern":"try { step(); } catch (e) { if (String(e).includes('yield is only valid')) { /* convert to generator or return */ } }","preventionTips":["Always declare generators with function*","Consume generators via next()/iteration","Strip yield from code moved into non-generator functions"],"tags":["javascript","generator","yield","syntax-error"],"backgroundTag":"invalid-yield-usage","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"}