-
-
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
Async typescript functions as actions #806
Comments
This comment has been minimized.
This comment has been minimized.
I investigated yargs as an option but frankly I think commander.js offer the best design. However that doesn't mean that commander.js does not have an issue in this regard. I am very experienced but quite new to JavaScript. It leaves a lot to be desired when compared to most other languages. With that said, I find myself in a position where I have to build an enterprise-quality command-line app that must run under node.js. The app will also offer a REST API interface to some functions. Obviously neither it, nor the CLI can be allowed to block the message/UI thread. commander.js seems to be a little schizophrenic in this regard. Is it a parser or a command dispatcher? It's a really nice parser. Dispatcher? No so much. Command actions definitely do not block the thread however there is no reasonable way to return anything from the handling function, including a Promise. I have evaluated a few options but none of them will allow me to sleep well at night after this thing goes live. So I have a suggested change:
I would suggest option 1 as it provides a clean separation between parsing and functional execution. I thought about forking this project and making the changes myself but I have to say my learning curve would make it too costly and that same learning curve would probably produce less than optimal source code. If you were not able to accept our pull request then we would be in the situation where we would have to merge your changes into our version, forever. This is just food for thought. For our project we may write a parser from the ground up using TypeScript and the semantics of commander.js in the spec. Another possibility will be to just write a parser specific to the apps requirements. By the way, if there is something I have wrong, please feel free to constructively enlighten me. |
I think this behavior is correct. Exactly, promise-like return is not provided through commander api though. Why not wrap commander with promise api manually? By the way, I also don't think that it is needed to do jobs simultaneously around commander initiation. Generally, commander initiation should be down at the top level of a problem or a system at the entry file, which, I think, is a best practice. |
I had the same issue, turned out it was a version problem. |
Can this pattern be used?
|
@chrisgo I'm sure it can, but the point of my question was if it is possible to use async await. |
|
I looked in the code. I see it is not prepared for async chaining at all. EventEmmiter is used to execute a command. Is a strategy pattern enough? Do contributors have plans to adjust it to be chainable? |
I came across this issue on Yargs to improve their async support. Some interesting detail on possible code patterns: yargs/yargs#1420 |
Same problem. Oddly, some async tasks run to completion. Others do not. In particular, waiting on a promise that never resolves exits with status 0, which is a big no-no. version 3.0.2 |
Cheese it:
|
I am wondering about option 2 from #806 (comment) So, we would add a Might that work for people wanting to use async? |
I have added a draft Pull Request to add |
Added See https://github.com/tj/commander.js#action-handler-subcommands |
Since we can use `parseAsync` for async functions so the action can be asynchronous. related to tj#806
Since we can use `parseAsync` for async functions so the action can be asynchronous. related to tj#806
Since we can use `parseAsync` for async functions so the action can be asynchronous. related to tj#806
Since we can use `parseAsync` for async functions so the action can be asynchronous. related to tj#806
Since we can use `parseAsync` for async functions so the action can be asynchronous. related to tj#806
Since we can use `parseAsync` for async functions so the action can be asynchronous. related to #806
Since we can use `parseAsync` for async functions so the action can be asynchronous. related to tj/commander.js#806
When I pass an async typescript function into a command's action() method, the async function is called correctly, but is not run to the end, as the program does not wait when awaiting another function. It simply ends execution. I've seen a similar question here, but it was not answered, as far as I understand.
Consider this example:
If you run the example above, you can see that finished is called but the download is never finished. Calling await run() without the help of commander works, though.
So, is it possible to use commander with typescript's async functions?
The text was updated successfully, but these errors were encountered: