-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
I'm using coffeescript with BlueBird and do this: program.command().action ->
do async ->
foo() |
@AGresvig have you found any good ways to do such thing? |
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)) |
@josser I'm now using Babel and ES7
Another option I tried with some success was Bluebird's @ProTip Nice one! Not having to write the error handling every time is very convenient |
Just tried this with node 8, however it does not seem to work. I have it set up like the following:
However, this simply prints EDIT: Nevermind, I misunderstood the purpose of |
Closed, seems the issue isn't directly related to commander |
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:program.command('myCmd').action(program => co(function* (program) {...}).catch(err => console.log(err.stack)) );
and
program.command('myCmd').action(co.wrap(function* (program) { .. }));
The problem with 1) is that the
program
parameter isn't passedThe 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!
The text was updated successfully, but these errors were encountered: