0

I need to write a callback function in Javascript that returns a simple true or false that will determine if execution continues in an SFTP call. (I'm using the SSH2 nodejs library and the hostVerifier configuration option, but this is probably an issue one can find in a lot of situations).

So I'm doing something similar to this:

conn.on('ready', function() {

            // do some stuff here


}).connect({
            host,
            port: 22,
            username,
            password,
            hostHash: 'md5',
            hostVerifier: validateFn,
});

where validateFn looks something like:

const validateFn = async function(hash) {
    try {
            const myFile = await s3.getObject({
                Bucket: "my-bucket",
                Key: `${filename}`,
            }).promise();

            // bla, bla ... do some more stuff here

        } catch (e) { 
            return false;
        }

        return true;
};

The validateFn call needs to be async because it's doing some remote callouts and I need to await on the response before proceeding. The problem is that validateFn will return a promise-wrapped boolean and not the simple boolean that SSH2 expects and the whole thing doesn't work right.

What ways can you get around this problem and get a callback to return the simple true/false that is expected?

iu.david
  • 181
  • 2
  • 8
  • 1
    You can't synchronously process data whose response will come in the future - that'd require time-travel. If https://github.com/sanketbajoria/ssh2-promise says `hostVerifier` accepts a callback, I'd use that – CertainPerformance Mar 25 '20 at 23:59
  • Even the standard [`ssh2`](https://www.npmjs.com/package/ssh2) module supports `hostVerifier` with a callback – Phil Mar 26 '20 at 00:00
  • I'm not sure what "You can't synchronously process data whose response will come in the future" means. I thought the whole point of await was to halt the method until a promise was resolved. – iu.david Mar 26 '20 at 00:28

0 Answers0