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!
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 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
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.
Closed, seems the issue isn't directly related to commander
Most helpful comment
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:
I actually wrap commands with a method that catches errors: