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
Allow command handler to accept a promise #510
Comments
Could this be accomplished using gulp's async-done library? If so I can try to take a stab at a PR. Thanks! |
@silenceisgolden I think we should be able to pull this off without pulling in any dependencies; once the command itself is invoked, its now the application's responsibility to handle things -- so it's perfectly fine for the handler to be async. I think, basically, we'd just need to update the argument validation and invocation of a command handler to understand a promise. I love this idea (thanks @vjpr) and would love your help with the patch @silenceisgolden. |
@bcoe Sounds good, I'll see what I can put together. |
I'd like to see this as well. For now I'm getting by with this https://github.com/eddywashere/yargs-promise. |
I also ended up doing an user-land solution for this problem just like @eddywashere, but the solution integrates with yargs, instead of changing its internals. |
This also seems to get the job done without needing to modify every command builder with a wrapper. That said, native support would be nice. process.on('unhandledRejection', function onUnhandledRejection(err) {
var cmd = process.argv.slice(2).join(' ');
console.error('Failed to execute "' + cmd + '"');
console.error(err.toString());
if (argv.debug) {
console.error(err);
}
// eslint-disable-next-line no-process-exit
process.exit(1);
}); |
To those subscribed: I've submitted PR #1001 to address this. Reviews are appreciated. |
Hi, I've tested the 10.1.0 with a command like this: .command([ 'replicate', 'r' ], 'Clones a repository', async (_yargs) => require('./lib/git/replicate').replicate(_yargs))) When the function replicate contains an
Am I doing something wrong ? |
@quilicicf The third argument is the command builder, not the command handler https://github.com/yargs/yargs/blob/master/docs/api.md#commandcmd-desc-builder-handler |
Thanks @zenflow! I don't know if my use case is one that yargs should support or if I should simplify things a bit. In any case, it could be good to change the initial comment to show that it's the command handler (4th position) that's been changed ? |
I did not notice that, but you're right! If @vjpr (or someone else with the permission) sees this, could you please correct the examples in the issue description for posterity? I.e. Insert a noop
It will be more complicated to make the command builder async. But you should open an issue for it! Is the new feature for async command handlers in v10.1.0 working ok for you? Some feedback over on the PR would be greatly appreciated! #1001 (comment) |
Well I don't use it in my use case but I can test it if that helps. |
we support async functions in command handlers now, we won't likely be adding this functionality to builders, however we are talking about making an async version of yargs: see: #1453 |
Not the expected outcome but damn, I'm excited about it. |
This would allow much easier use of ES7 async/await.
Workaround
Use IIFE.
The text was updated successfully, but these errors were encountered: