我正在使用 shellJs 和 Node WebKit 在 Android 平板电脑上工作。我想做的是使用 shelljs 来 Fastboot 读取引导加载程序变量(产品)。但在 Fastboot 中,如果没有连接设备,则返回“等待设备”并且连接保持打开状态。 我想做的是让这个命令运行最多 5 秒,然后如果输出 != 'Soho' 抛出错误并取消函数。
这是我当前的代码
$('#fbbtn').click(function(){
var getVar = terminal.exec('fastboot -i 0x1949 getvar product');
var getSerial = terminal.exec('fastboot -i 0x1949 devices');
var test getVar.output.match(/Soho/g);
if(test=='Soho'){
fbSerial=getSerial.output.substr(0,16);
}
else alert('ERROR');
});
我知道 shelljs 中的 exec 提供了 {async:true} 但不太理解 Promise,并且找不到关于它们的很好的文档。希望有人可以在这里教育我
最佳答案
尝试异步运行您的子进程,等待 5 秒,然后终止它。
var getvar = terminal.exec('fastboot -i 0x1949 getvar product', { async: true });
getvar.stdout.on('data', function(data) {
var product = data;
//Use the aquired value here
});
setTimeout(function() {
getvar.kill();
}, 5000);
您必须像我的示例一样监听数据,然后检查它是否是您想要的字符串或“等待设备”。
关于javascript - 如何使用 shelljs 使 Node 中的错误超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25473715/