Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronous spawn in node.js

Tags:

node.js

I would like to create a child process in node, and block in a fiber until the process has terminated. They way I have understood it, it should look something like this:

var child_process = require ("child_process");
var Fiber = require ("fibers");
var Future = require ("fibers/future");

var ls = Fiber (function () {
    var lsproc = child_process.spawn ("ls");
    var lsonSync = Future.wrap (lsproc.on);

    console.log ("return: " + lsonSync ("exit").wait ());
}).run ();

The response from node is:

TypeError: Object #<Object> has no method 'emit'

I assume this has something to do with the fact that I am wrapping an instance method instead of a function, but I am not sure how proceed.

like image 711
wkz Avatar asked Dec 06 '25 13:12

wkz


1 Answers

Sometimes you need to ask the question for the answer to pop into your head.

Binding the on-method to lsproc before wrapping it in a future solves the problem:

var child_process = require ("child_process");
var Fiber = require ("fibers");
var Future = require ("fibers/future");

var ls = Fiber (function () {
    var lsproc = child_process.spawn ("ls", ["/etc"]);
    var lsonSync = Future.wrap (lsproc.on.bind (lsproc));

    console.log ("return: " + lsonSync ("exit").wait ());
}).run ();
like image 62
wkz Avatar answered Dec 10 '25 11:12

wkz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!