[英]Run synchronous tasks using node.js on windows
I am using the Node FFI module and am trying to run sync tasks on Windows. 我正在使用Node FFI模块,并试图在Windows上运行同步任务。 I can successfully run a task using the following code. 我可以使用以下代码成功运行任务。
var ffi=require('ffi')
var nativeC = new ffi.Library("Kernel32", {
"WinExec": ["int32", ["string"]]
});
nativeC.WinExec('ls -lrt');
I presume this is the way to execute sync tasks, but this code always exits after the 1st 'ls -lrt' command; 我认为这是执行同步任务的方式,但是此代码始终在第一个'ls -lrt'命令之后退出; if I chain a few more commands, they won't work. 如果我再链接几个命令,它们将无法工作。 So, is there a callback function over here, in the FFI module, or another way I can chain commands in node.js on Windows so they run in sync, one after the other. 因此,在这里,FFI模块中是否有回调函数,或者是我可以在Windows上的node.js中链接命令的另一种方式,以便它们可以一个接一个地同步运行。
I'm not sure you need WinExec to run a windows command. 我不确定您是否需要WinExec来运行Windows命令。 As Jonathan pointed out, ls isn't available. 正如乔纳森指出的那样,ls不可用。
However, if you want to chain commands you could use async.js and exec like this: 但是,如果要链接命令,可以使用async.js和exec,如下所示:
var
async = require('async');
exec = require('child_process').exec,
commands = [ 'dir /w', 'echo test'];
var executeCommand = function(command, callback){
exec(command, function (err, stdout, stderr) {
if(err) return callback(err);
console.log(stdout);
callback();
});
};
async.eachSeries(commands, executeCommand, function(err){
console.log('error: ' + err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.