Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Promises, (async/await) #21

Open
misogihagi opened this issue Aug 20, 2020 · 2 comments
Open

Support for Promises, (async/await) #21

misogihagi opened this issue Aug 20, 2020 · 2 comments

Comments

@misogihagi
Copy link

misogihagi commented Aug 20, 2020

keygen function is normal function not Promise or async function. I confused for a while and realized it may want some async methods.

code

keygen({
  location: location,
  comment: comment,
  password: password,
  read: true,
  format: format
}, function(err, out){
    console.log('111')
    fs.writeFile('aaa',out.key)
});
console.log('222')

result

222
111
@DeanKamali
Copy link

If you are trying to use async / await, you could use pify package to Promisify a callback-style function, here is an example

const pify = require('pify');

(async () => {
    let sshkeys = await pify(keygen)({
            location: location,
            comment: comment,
            password: password,
            read: true,
            format: format
        });
   console.log('sshkeys', sshkeys)
})();

@micalevisk
Copy link

micalevisk commented Feb 1, 2022

You can use a promise-based API in my fork as well as the callback-based one

const keygen = require('@micalevisk/ssh-keygen')
async function runPromiseVersion() {
  console.log('Generating key pair')

  try {
    const out = await keygen({
      comment: 'john@doe.com',
      read: true,
    }); // just supply the second parameter here (as usual) if you don't want to return a Promise

    console.log('Done generating key pairs');
    console.log(out.key);
    console.log(out.pubKey);
  } catch (err) {
    console.log('There was a problem:', err);
  }
}

it doesn't have any dependencies.

@ericvicenti ericvicenti changed the title async problem Support for Promises, (async/await) Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants