我对节点很陌生,遇到了按顺序运行某些函数和使用回调的问题。我曾尝试使用异步模块,但是,我认为我错过了一些东西,因为它不按顺序执行我的功能。按顺序运行nodejs中的函数
在最简单的话来说,我想在网址参数,然后的形式来获取一些数据:1. 它们写入一个文件 2 LPR打印出来 3.删除文件
我当前的代码在打印之前删除文件。在步骤2之前的步骤3.是否有人会提供一些关于如何最好地执行这些操作的建议?提前致谢。
router.get('/zplprint/:barcode/:zpl', function(req, res) {
var zpl = req.params.zpl;
var filename = appDir + "/uploads/" + req.params.barcode + ".zpl";
console.log(req.params.zpl);
res.send("received zpl: " + req.params.zpl);
async.series([
function(callback){
fs.writeFile(filename, zpl, function(err) {
if(err) {
callback(err);
return;
}
console.log("The file was saved! to "+filename);
callback();
});
},
function(callback){
CupsPrinterName = nconf.get('Print:PrinterName');
console.log(CupsPrinterName);
var cmd = 'lpr -P ' + CupsPrinterName + ' -o raw ' + filename;
exec(cmd, function(error, stdout, stderr) {
// command output is in stdout'
console.log(cmd);
console.log("file printed");
});
callback();
},
function(callback){
fs.unlink(filename, function (err) {
console.log(' deleting ' + filename);
});
callback();
}
]);
});
我知道我失去了一些东西。这似乎已经成功了。谢谢 – neohusky