我正在尝试在事件驱动的项目中使用异步等待,但出现以下错误:
tmpFile = await readFileAsync('tmp.png');
^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
到目前为止,我有以下代码(简化):
const fs = require('fs');
const dash_button = require('node-dash-button');
const dash = dash_button(process.env.DASH_MAC, null, 1000, 'all');
function readFileAsync (path) {
return new Promise(function (resolve, reject) {
fs.readFile(path, function (error, result) {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
};
async function main() {
dash.on("detected", function () {
tmpFile = await readFileAsync('tmp.png');
console.log(tmpFile);
});
}
main();
我的问题不是真正的下面的库,而是了解异步等待的基本原理并在事件驱动的脚本中使用它。我不太了解这是范围问题还是其他问题。
我正在为亚马逊破折号按钮使用以下事件驱动的库:https : //github.com/hortinstein/node-dash-button
谢谢,
安迪
您的异步功能不正确。它需要在回调中:
function main() {
dash.on("detected", async function () {
tmpFile = await readFileAsync('tmp.png');
console.log(tmpFile);
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句