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

Best way to make action async? #505

Closed
AGresvig opened this issue Feb 22, 2016 · 6 comments
Closed

Best way to make action async? #505

AGresvig opened this issue Feb 22, 2016 · 6 comments

Comments

@AGresvig
Copy link

In the pursuit of cleaner and more readable code, I want to leverage ES6 and leave callback hell, so I'm trying to get my .action() callbacks to become async methods, but I haven't yet found the optimal route.

I've tried wrapping the CB with co in two ways:

  1. program.command('myCmd').action(program => co(function* (program) {...}).catch(err => console.log(err.stack)) );
    and
  2. program.command('myCmd').action(co.wrap(function* (program) { .. }));

The problem with 1) is that the program parameter isn't passed

The problem with 2) is that errors are swallowed...

I'd really like to get this working as it yields much nicer code in my use case - involving a lot of http requests and also waiting for user input using the co-prompt library..

Is it a better option altogether perhaps to wrap program.Command.prototype.action somehow?

@tj I know this is a piece of cake for you - pls enlighten me!

@ProTip
Copy link

ProTip commented Mar 6, 2016

I'm using coffeescript with BlueBird and do this:

program.command().action ->
  do async ->
    foo()

@josser
Copy link

josser commented Sep 6, 2016

@AGresvig have you found any good ways to do such thing?

@ProTip
Copy link

ProTip commented Sep 6, 2016

I have since moved to TypeScript but this should be applicable to Babel etc. Commander does not need a return or anything, so I just pass the async method directly to it:

program.command(async function() { } )

I actually wrap commands with a method that catches errors:

withErrors = (command: (...args) => Promise<void>)=> {
    return async (...args: any[])=> {
      try {
        await command(...args)
      } catch(e) {
        console.log(chalk.red(e.stack))
        process.exitCode = 1
      }
    }
  }

async function myComand() { }

program.command(withErrors(myCommand))

@AGresvig
Copy link
Author

AGresvig commented Sep 6, 2016

@josser I'm now using Babel and ES7 async handlers:

  async handler(argv) {
    try {
      await doThingsWith(argv);
    } catch (e) {
      console.error(e.stack || e);
    }
  }

Another option I tried with some success was Bluebird's coroutine

@ProTip Nice one! Not having to write the error handling every time is very convenient

@bduff9
Copy link

bduff9 commented Jun 8, 2017

Just tried this with node 8, however it does not seem to work. I have it set up like the following:

async function handler (env, options) {
	await someAsyncFunc();
}

program
	.version('0.0.1')
	.option('-d, --download', 'Download only')
	.action(handler);

console.log('Finished!');

However, this simply prints Finished! and then exits. In fact, even --help does not work with this, unless I remove the action line and replace it with .parse(process.argv);. What am I doing wrong?

EDIT: Nevermind, I misunderstood the purpose of program.parse(process.argv); The above works perfectly if I add in this line prior to the console.log line. My mistake.

@roman-vanesyan
Copy link
Collaborator

Closed, seems the issue isn't directly related to commander

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

5 participants