Thursday, 16 March 2017

JavaScript - returning value from anonymous function parameter

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?



via Mike_M2

No comments:

Post a Comment