0

I am using npm 'powershell' package for executing PowerShell commands and reading related output. I want to write a function that would return standard command output (so that I could call the the function and use its return value in assertions etc.).

const PowerShell = require("powershell");

var myFunction = function (command) {
    let ps = new PowerShell(command);

    ps.on("error", err => {
        console.error(err);
    });

    ps.on("output", data => {
        console.log(data);
        //return data; <-- this does not work
    });

    ps.on("error-output", data => {
        console.error(data);
    });

    ps.on("end", code => {
        console.log("The end");
    });
};

I want myFunction to return data value (from standard output). However, I don't know how to do it properly. Could you please advise?

m87
  • 4,445
  • 3
  • 16
  • 31
Mike_M2
  • 87
  • 6
  • Go through one of a dozen 'return value from async function in node' questions on the site: tl; dr -> use a callback or a Promise – paqash Mar 16 '17 at 13:08
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – paqash Mar 16 '17 at 13:09
  • `Promisify` your function or pass in a `callback`. There are lot of existing resources on how to do this. – Sachin Mar 16 '17 at 14:25

1 Answers1

0

Look into how callbacks work. An example for your function would be

var myFunction = function (command, callback) {
    // code
    ps.on("output", data => {
        callback(data)
    });
    // code
};

myFunction('ls', function (data) {
    console.log('The callback data:', data);
});
Skabbi
  • 336
  • 2
  • 10
  • Skabbi, thanks. It works, however, it's not the complete solution. I would like myFunction to return a value, so that I can e.g. use it in an expect. I would like to assign the result of myFunction call to a variable and then use this variable in an expect. Is it possible? – Mike_M2 Mar 22 '17 at 13:12
  • You can unit test callback functions, how you do it depends on the library you are using. But you can't really return anything with callbacks because they are async, I'm not really sure what you want to do, but you could assign 'data' to another variable or you could check out the [async](http://caolan.github.io/async/) module – Skabbi Mar 22 '17 at 23:17
  • Skabbi thanks. Unfortunately, still I didn't manage to resolve the issue. I am using Jasmine framework. I just want to write the following expression: expect(commandOutput).toEqual('somevalue'). Placing the expect instead of console.log... does not work. Could you please give me some more hints? – Mike_M2 Mar 23 '17 at 09:29
  • I would say that teaching you how to unit test async code is outside the scope of the original question. It might be better to create a new question or reading over older questions, e.g. [Testing Asynchronous Callbacks with Jasmine](http://stackoverflow.com/questions/27344872/testing-asynchronous-callbacks-with-jasmine) – Skabbi Mar 23 '17 at 13:31