0

i have a small problem in creating javascript.

i have a js which spawn shell script(expect, which ssh another computer, and send some data back to nodejs.

i have problem in multithreading.

var cons = spawn('./somescript');
var content;
cons.stdout.on('data', function(block)
{
content += block;
mrdialist = JSON.parse(block);
....
console.log("" + block);
});
console.log("hjdfsdf");
here is some actions with loaded and parced data.

but when i try to execute jscript, it start from the beginning, and make all actions through loading data without loading and parsing, how can i continue execution of the code onlu, after execution

    console.log("" + block);
    });
    console.log("hjdfsdf");
4

1 回答 1

1

spawn 将异步执行。close您必须在发出事件时执行您的代码。

var cons = spawn('./somescript');
var content;
cons.stdout.on('data', function(block)
{
content += block;
mrdialist = JSON.parse(block);
....
console.log("" + block);
});

cons.on('close', function () {
    console.log("hjdfsdf");
    // manipulate content
    console.log("content", content);
});
于 2013-10-11T15:10:50.300 回答