i have event triggering metor.call():
meteor.call("runcode", mycode, function(err, response) { session.set('code', response); console.log(response); }); but runcode function inside server's metheor.methods has inside callback , can't find way make return response in above code.
runcode: function(mycode) { var command = 'pwd'; child = exec(command, function(error, stdout, stderr) { console.log(stdout.tostring()); console.log(stderr.tostring()); // want return stdout.tostring() // returning here causes undefined because runcode doesn't return }); // can't return here because don't have yet valuer of stdout.tostring(); } i'd way have exec callback return runcode without setinterval work, hacky way in opinion.
you should use future fibers.
see docs here : https://npmjs.org/package/fibers
essentially, want wait until asynchronous code run, return result of in procedural fashion, future does.
you find out more here : https://www.eventedmind.com/feed/ww3rqrhjo8flgk7ff
finally, might want use async utilities provided package : https://github.com/arunoda/meteor-npm, make easier.
// load future fibers var future=npm.require("fibers/future"); // load exec var exec=npm.require("child_process").exec; meteor.methods({ runcode:function(mycode){ // method call won't return immediately, wait // asynchronous code finish, call unblock allow client // queue other method calls (see meteor docs) this.unblock(); var future=new future(); var command=mycode; exec(command,function(error,stdout,stderr){ if(error){ console.log(error); throw new meteor.error(500,command+" failed"); } future.return(stdout.tostring()); }); return future.wait(); } });
Comments
Post a Comment