0

Edit: I do not have the luxury of having a different program be able to use await or .then. The other software gives no control over that, just the ability to call a function and receive its answer as is. There surely must be a way to do this. It's not clear to me from the "duplicate question" that any of those options described are viable. Thanks. /end edit.

I have tried just about every combination of aysnc, await and resolve I can think of but no matter what I do, I am unable to create a node.js function that returns the contents of an FTP directory correctly. I am expecting

console.log(
            abca(   
                    'address',
                    'user',
                    'password',
                    '/home/counterpoint/081SA/Tickets'
                )
    );

to basically display the returned file list but it always returns Promise { } instead like it doesn't want to wait despite instructions saying to wait. Ultimately, I need to make it available for another system to use which is why I need it to run correctly inside a function. Any help would be greatly appreciated.

Full code:

    "use strict";

var Client = require('ssh2-sftp-client');
var sftp = new Client();

console.log(
        abca(   
                'address',
                'user',
                'password',
                '/home/counterpoint/081SA/Tickets'
            )
);

 async function abca(host, user, pwd, path){
    const config = {
        host: host,
        username: user,
        password: pwd
    };
    
    const files = await new Promise( (resolve, reject) => {      
        resolve(
           sftp.connect(config).then(() => {
               return sftp.list(path);
           }).then(data => {
                //console.log(typeof data);
                //console.log(data);
                return JSON.stringify(data) ;
            })
        );
    });
    
    return files;
}

Regards,

Andrew

Andrew S
  • 1
  • 1
  • ALL `async` functions return a promise. So, `abca()` is ALWAYS going to return a promise. Your caller will have to use `.then()` or `await` on the return value from `abca()`. – jfriend00 Oct 17 '22 at 22:07
  • Plus you're also using a promise anti-pattern here by wrapping an existing promise in another promise. Don't do that. Just return the promise you already have. – jfriend00 Oct 17 '22 at 22:08

0 Answers0