Something tells me that it is necessary to use callback to get the results of the function, but I can not figure out how to use this method. My code is as follows:

var conn = new Client(); conn.on('ready', function() { conn.exec(execomand, function(err, stream) { if (err) throw err; stream.on('close', function(code, signal) { console.log('Stream closed with code: ' + code + ', signal: ' + signal); conn.end(); }).on('data', function(data) { console.log(data.toString()); }).stderr.on('data', function(data) { console.log('STDERR: ' + data); }); }); }).connect({ host: IPaddress, username: loginname, password: loginpass, port: SSHport }); 

How do I access the contents of data in the rest of the code?

    1 answer 1

    To simply access it is enough to define a variable at the very beginning, for example, to var conn = new Client (); and then write the data there, if a kalbek function is needed, then it becomes a bit more complicated, although I don’t understand why it is needed here, in short, the kalbek function of the function is done like this

     function myFunc (data, callback) { тут куча кода, возможно даже асинхронного callback(returndata)} 

    and it should be called like this, for example, res is a web server return object

     res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'}); myFunc(data, function(err, res_data){ if(data) { res.end(res_data); } if(err) { var fatalerror = "всё сломалось"; res.end(fatalerror); } })