سؤال

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");
هل كانت مفيدة؟

المحلول

spawn will be execute asynchrounsly. you have to execute your code when event close will be emit.

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);
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top