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.
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 ();