Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将函数输出传递给另一个函数_Javascript_Casperjs - Fatal编程技术网

Javascript 如何将函数输出传递给另一个函数

Javascript 如何将函数输出传递给另一个函数,javascript,casperjs,Javascript,Casperjs,最近在玩casperjs,没有完成下面的代码,正在使用child_进程,需要将函数输出传递给另一个函数,有什么想法吗 success call变量范围仅限于success函数,我不能在代码中的任何地方使用它 casper.repeat(3, function() { this.sendKeys(x('//*[@id="text-area"]'), testvalue.call(this)); // testvalue.call(this) dosnt input anything he

最近在玩casperjs,没有完成下面的代码,正在使用child_进程,需要将函数输出传递给另一个函数,有什么想法吗

success call变量范围仅限于success函数,我不能在代码中的任何地方使用它

casper.repeat(3, function() {
    this.sendKeys(x('//*[@id="text-area"]'), testvalue.call(this)); //  testvalue.call(this) dosnt input anything here
})

casper.echo(testvalue.call(this)); // Print output successfully

function testvalue() {
    var spawn = require("child_process").spawn
    var execFile = require("child_process").execFile
    var child = spawn("/usr/bin/php", ["script.php"])

    child.stdout.on("data", function (data) {
        console.log(JSON.stringify(data)); // Print output successfully
        return JSON.stringify(data); // Problem is here i cant use Data any where in code except this scope
    })
}

由于
spawn
是一个异步进程,因此需要对
testvalue
使用回调。在事件处理程序中返回某些内容不会从
testvalue
返回

另一个问题是,您需要留在CasperJS控制流中。这就是为什么我使用
testvaluedone
来确定生成的进程是否执行完毕,并且我可以
completeData

casper.repeat(3, function() {
    var testvaluedone = false;
    var completeData = "";
    testvalue();
    this.waitFor(function check(){
        return testvaluedone;
    }, function then(){
        this.sendKeys(x('//*[@id="text-area"]'), completeData);
    }); // maybe tweak the timeout a little
});

var testvaluedone, completeData;
function testvalue() {
    var spawn = require("child_process").spawn;
    var execFile = require("child_process").execFile;
    var child = spawn("/usr/bin/php", ["script.php"]);

    child.stdout.on("data", function (data) {
        completeData += JSON.stringify(data);
    });
    child.on("exit", function(code){
        testvaluedone = true;
    });
}

因为数据作为函数的参数是作用域的一部分。如果您返回类似
return{json:json.stringify(data),data:data}
的内容,这将使函数返回一个包含这两个内容的对象,这样您就可以在这个范围之外访问
data
。这对我不起作用,您能提供完整的示例吗?我添加了对
testvalue
的实际调用。