The result
is populated in the exec
function, but it never gets back to the main thread... what am I doing wrong?
var Fiber, exec, execSync;
exec = require("child_process").exec;
Fiber = require('fibers');
execSync = function(cmd) {
var cmdExec, final;
cmdExec = function(cmd) {
var fiber,
_this = this;
fiber = Fiber.current;
exec(cmd, function(se, so, e) {
var result;
fiber.run();
result = se + so + e;
return result;
});
return Fiber["yield"]();
};
final = '';
Fiber(function() {
return cmdExec(cmd);
}).run();
return final;
};
console.log(execSync('ls ..'));
there's a few problems in that code.. Here's some code that does what you want:
var Fiber, exec, execSync;
exec = require("child_process").exec;
Fiber = require('fibers');
execSync = function(cmd) {
var fiber = Fiber.current;
exec(cmd, function(err, so, se) {
if (err) fiber.throwInto(err);
fiber.run(se + so);
});
return Fiber.yield();
};
Fiber(function() {
console.log(execSync('ls ..'));
}).run();
The big problem is that you seem to be mixing up the role of run
and yield
. Basically yield
suspends a Fiber and run
will resume (or start it for the first time). What you should be doing is running any code that needs to call execSync
inside a Fiber, and then execSync
will grab a reference to the current fiber and then call Fiber.yield()
. When the exec
call returns the fiber is resumed with fiber.run()
.
The other smaller issue is some confused parameters to the callback for exec
. The parameters are err, stdout, stderr
.