Support promises as handlers
jsumners opened this issue · comments
Line 169 in 52909ec
With the above, the following will not return an error:
prog.command('new <val>').action(async val => {
if (val === 'foo') throw Error('swallowed by the ether')
console.log('yay')
})
Hey,
For now, you'd have to run your program "lazily" so that you can execute any async handler(s) in the appropriate function. I do plan on enabling async handlers, but it'd need to be a major (2.0) release.
prog.command('new <val>').action(async val => {
if (val === 'foo') throw Error('swallowed by the ether')
console.log('yay')
})
async function init() {
const { args, name, handler } = prog.parse(process.argv, { lazy: true });
console.log('Running command:', name);
await handler.apply(null, args);
}
Glad it's on the todo list. Thank you.
Well, I misread this actually. And confused myself in the process :D
Anywhere that async
is supported, you can use an async/await handler already. The problem you're running into is that you are throwing an Error instead of handling it directly. This is a CLI context and not any other.
Put differently, you need to handle your error directly rather than throwing it up for the parent context to handle... because there is none --- unless you parse lazily and provide one as I've shown.
Closing this since async handlers are supported already. At least 90% of my Sade programs are using async functions.
Hope that helps