{"record":{"id":"057f16de8e2c7573","repo":"karatelabs/karate","slug":"waitforoutput-requires-a-function-argument","errorCode":null,"errorMessage":"waitForOutput requires a function argument","messagePattern":"waitForOutput requires a function argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/process/ProcessHandle.java","lineNumber":532,"sourceCode":"                JavaCallable listener = (JavaCallable) args[0];\n                onStdErr(line -> {\n                    try {\n                        listener.call(ctx, line);\n                    } catch (Exception e) {\n                        logger.warn(\"onStdErr listener error: {}\", e.getMessage());\n                    }\n                });\n                return this;\n            };\n            case \"waitSync\" -> (JavaCallable) (ctx, args) -> {\n                if (args.length > 0 && args[0] instanceof Number) {\n                    return waitSync(((Number) args[0]).longValue());\n                }\n                return waitSync();\n            };\n            case \"waitForOutput\" -> (JavaCallable) (ctx, args) -> {\n                if (args.length == 0 || !(args[0] instanceof JavaCallable)) {\n                    throw new RuntimeException(\"waitForOutput requires a function argument\");\n                }\n                JavaCallable predicate = (JavaCallable) args[0];\n                long timeout = args.length > 1 && args[1] instanceof Number\n                        ? ((Number) args[1]).longValue() : 0;\n                return waitForOutput(line -> {\n                    Object res = predicate.call(ctx, line);\n                    return Boolean.TRUE.equals(res);\n                }, timeout);\n            };\n            case \"close\" -> (JavaCallable) (ctx, args) -> {\n                boolean force = args.length > 0 && Boolean.TRUE.equals(args[0]);\n                close(force);\n                return null;\n            };\n            case \"signal\" -> (JavaCallable) (ctx, args) -> {\n                if (args.length > 0) {\n                    signal(args[0]);\n                }","sourceCodeStart":514,"sourceCodeEnd":550,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/process/ProcessHandle.java#L514-L550","documentation":"ProcessHandle.waitForOutput(predicate) blocks until a stdout line makes the JS predicate return true. The binding requires the first argument to be a function; missing or non-function arguments throw this RuntimeException. A numeric second argument is the optional timeout in millis.","triggerScenarios":"Calling process.waitForOutput() with no arguments (it is not a plain wait), or passing a string/regex/object instead of a function as the first argument.","commonSituations":"Migrating from a wait(timeout) call and assuming waitForOutput() takes a number; passing a regex literal directly hoping the library applies it; forgetting to wrap a condition in a function.","solutions":["Wrap the condition: p.waitForOutput(line => line.includes('READY'))","Optionally pass a second numeric argument as timeout: waitForOutput(pred, 5000)","If you just want to wait for exit, use wait() / waitSync() instead of waitForOutput"],"exampleFix":"// before\np.waitForOutput('READY');\n// after\np.waitForOutput(line => line.includes('READY'), 10000);","handlingStrategy":"type-guard","validationCode":"if (typeof predicate !== 'function') { throw new Error('predicate must be a function'); }","typeGuard":"function isFn(x) { return typeof x === 'function'; }","tryCatchPattern":"try {\n  p.waitForOutput(pred, 10000);\n} catch (e) {\n  karate.log('waitForOutput failed: ' + e.message);\n}","preventionTips":["Remember waitForOutput's first argument is always a function, not a timeout or regex","Use waitSync()/wait() for plain time-based waits","Pass a second numeric argument for the timeout instead of relying on defaults"],"tags":["process","javascript","callback","argument-type"],"backgroundTag":"invalid-argument-value","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"}