0

I am new to handling promises or async function calls.In my code I am trying to execute command on one remote server and get values in one of my javascript function. I am using below code ,

var SSH = require('ssh2');
var funcdata = getData()
console.log(funcdata)

function getData() {
    var data = 'nodata';
        var ssh = new SSH({
            host: 'xxx.xxx.xxx.xxx',
            user: 'username',
            pass: 'password'
        });
        ssh.exec('ls', {
            out: function(stdout) {data += stdout.toString()},
            exit: function(code) {
                console.log(data) // this works here and i want to return this data value               
            }
        }).start();
        ssh.on('error', function(err) {});
    return data 
}

When i execute, this returns me funcdata as initial value only that is 'nodata'. How can i return the vales which is return from exit in ssh call ? please suggest something.

Thanks

usersam
  • 1,125
  • 4
  • 27
  • 54
  • Please add more context to the question, as well as what functions are async here – GalAbra Apr 26 '20 at 07:09
  • Please read the [duplicate](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323) where it describes your options for communicating an asynchronously retrieved value to the caller of a function (hint, you cannot return it directly). Your choices are using a promise, a callback or an event. – jfriend00 Apr 26 '20 at 07:16

0 Answers0