【发布时间】:2015-07-11 06:05:25
【问题描述】:
我正在我的 Raspberry Pi 上构建我的第一个 node.js 应用程序,我用它来通过 LIRC 控制空调。当您想提高交流单元的温度时,将调用以下代码。它每 250 毫秒发送一个 LIRC 命令,具体取决于您希望将其增加多少度。此代码按预期工作。
var iDegrees = 5;
var i = 0;
var delay = 250 // The delay in milliseconds
function increaseTemperatureLoop(){
i++;
//lirc_node.irsend.send_once("ac", "INCREASE", function() {});
console.log(i);
// Call the fucntion/loop again after the delay if we still need to increase the temperature
if (i <= iDegrees){
timer = setTimeout(increaseTemperatureLoop, delay);
}
else {
res.json({"message": "Success"});
}
}
// Start the timer to call the recursive function for the first time
var timer = setTimeout(increaseTemperatureLoop, delay);
我很难使用 node.js 的异步特性。一旦我的递归函数完成,我将我的 json 返回到浏览器,如上面的代码所示。按照习惯,我觉得我应该在下面的初始函数调用之后在一行代码中返回 json,但显然这不会等待所有 LIRC 调用成功 - 将它放在函数内部似乎很愚蠢:
var timer = setTimeout(increaseTemperatureLoop, delay);
res.json({"message": "Success"});
如果在我的 LIRC 发送完成之后,但在我想将我的 json 发送回浏览器之前,我还有很多其他事情要做,该怎么办?或者如果该代码块抛出错误怎么办......
我的第二个问题是,我如何正确地将 LIRC 调用包装在 try/catch 中,然后如果出现错误,停止递归调用,将错误传递回去,然后将其传递回浏览器实际的错误信息:
res.json({"message": "Failed"});
【问题讨论】:
标签: node.js recursion raspberry-pi settimeout