I am still new to javascript. My problem: I check a folder on a server via ssh. If the folder is empty then I want to return a value (true / 1 / ...). But I just can not get back a value from my function. It always comes back "undefined" ...
What am I doing wrong? I already read something about promise / callback, but even these approaches did not work ... Thank you in advance =)
async function CheckIfDirectoryIsNotEmpty() {
let value;
let Client = require('ssh2-sftp-client');
let sftp = new Client();
sftp.connect({
host: 'xxxxx',
port: 'xx',
username: 'xxxxxxxx',
password: 'xxxxxxxx'
}).then(() => {
return sftp.list('/opt/cm-shared-data/example/')
}).then((data) => {
if(data.length == 0){
value = 0;
}
else { value = 1;}
sftp.end();
return value;
});
}
Call the method:
async function start() {
var a = CheckIfDirectoryIsNotEmpty();
console.log(a); // "undefined"
}