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?