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:
The big problem is that you seem to be mixing up the role of
runandyield. Basicallyyieldsuspends a Fiber andrunwill resume (or start it for the first time). What you should be doing is running any code that needs to callexecSyncinside a Fiber, and thenexecSyncwill grab a reference to the current fiber and then callFiber.yield(). When theexeccall returns the fiber is resumed withfiber.run().The other smaller issue is some confused parameters to the callback for
exec. The parameters areerr, stdout, stderr.