I am writing a function in nodejs to send print command to the macos. The problem is that my print command is sent successfully but I want to wait for the output received before moving forward.
My code is as follows
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;
});
}
};
The problem here is that the function executeQuery is not executed completely and the result is returned i.e. undefined. How do I make my program wait for the function to execute properly?