0

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?

VIBHOR GOYAL
  • 473
  • 1
  • 6
  • 22
  • Possible duplicate of https://stackoverflow.com/questions/55687275/undefined-value-after-returning-an-array-of-values-from-a-mysql-query-in-a-diffe/55688488#55688488 – Sreehari Apr 26 '19 at 01:57

2 Answers2

1

executeQuery is not working as expected because you have mixed Async-Await with callbacks.

You cannot use Async-await syntax with callback. You have to promisify your callback function as below.

    function(command){
        return new Promise(resolve, reject){
             exec(command, function (error, stdout, stderr) {
             output = {stdout, error, stderr};
             if (!stdout || stderr || error)
                 reject();
             else
                 resolve();
             })
        }
    }
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
Sreehari
  • 1,328
  • 11
  • 29
0

Okay, it seems like this is wrong

   await this.printUnix(options).then(
      response => {
        return response
      }
    ).catch(error => {
      return false
    });
  },

When you use async/await. you can't use promise callbacks .then or .catch (probably)

Try changing your code to something like this

 print: async function (options) {
   try {
     return await this.printUnix(options)
     } catch (error) {
      return false
  },
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210