Я пишу функцию в nodejs для отправки команды печати в macos. Проблема в том, что моя команда печати успешно отправлена, но я хочу дождаться вывода, прежде чем двигаться вперед.
Мой код выглядит следующим образом
const printer = require("node-native-printer");
const exec = require("child_process").exec;
module.exports = {
print: async function (options) {
await this.printUnix(options).then(
response => {
return response
}
).catch(error => {
return false
});
},
printUnix: async function (options) {
if (!options.filePath)
return Error('File path not specified');
let command = 'lp ';
let unixOptions = [];
await Object.keys(options).forEach(value => {
switch (value) {
case 'duplex':
if (options[value] === 'Default')
command = command + '-o sides=one-sided ';
else
command = command + '-o sides=two-sided-short-edge ';
break;
case 'color':
if (options[value])
command = command + '-o blackplot ';
break;
case 'landscape':
if (options[value])
command = command + '-o orientation-requested=4 ';
else command = command + '-o orientation-requested=3 ';
break;
}
});
command = command + options.filePath;
return await this.executeQuery(command);
},
executeQuery: async function (command) {
exec(command, function (error, stdout, stderr) {
output = {stdout, error, stderr};
if (!stdout || stderr || error)
return false;
else
return true;
});
}
};
Проблема здесь в том, что функция executeQuery не выполняется полностью, а результат возвращается, т.е. не определено. Как заставить мою программу ожидать правильного выполнения функции?