-
-
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
Add parseAsync #1118
Add parseAsync #1118
Conversation
To do:
Note: I am not a Promise expert. If there are bad patterns or assumptions in this code, then please let me know! |
I think I agree that the problem traces to the end of the event listener in action(), but if I back up for a second I have two other concerns. The simplest one is, wouldn't it be simpler to register a new event that is emitted after fn.apply() resolves/returns. I know I wrote an async action. If you give me an event to wait for, I can promisify it in a couple lines of code and only call process.exit() after it resolves, and I think I'd be perfectly okay with that. In fact if I have multiple commands, which I do, that will still result in simpler code than my weird setInterval workaround. |
I think we have two different problems @jdmarshall. I was focused on caller being able to manage async action handlers, and (say) Your problem (with setInterval workaround) is presumably that a pending promise alone does not keep node running, which is potentially unexpected: I didn't encounter the early-exit problem because I was using |
@jdmarshall Do you have a small example of a problem async action that would not run correctly for you if you could use await, without additional timer? (i.e. what is the code doing asynchronously that node does not recognise will resolve later?) |
Not at present. What drew me here was that I had an async action that seemed to be working fine, performing a lot of async calls (throttled uploads), and then I added a second async task to the end of that action and it never ran. I haven't been back to it yet to come up with a repro case. |
Here is a javascript example for node 8 or up with
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested this PR and it works for me. I am on node v.10.14.2.
I am running async actions against a lengthy database update script.
Thanks @blendsdk |
Some noise in TOC due to changes in plugin which maintains TOC.
Added README. Have a test and typings. This is a non-breaking addition so can go in v4, but I am happy to leave it for v5 if preferred. |
This is the only PR I am interested in including before v4.1, but happy to leave it for v5 whether for safety or to allow more time to review @abetomo . No worries. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thanks @abetomo. I'll start preparing 4.1 soon. |
CHANGELOG updated for 4.1 on develop branch. Ready to release to master, version number and tag still to be updated.
I'll add an issue for last change to Chinese README but we can add that after release. |
Pull Request
Problem
People are using async routines from TypeScript and Javascript, and want to declare async action handlers. For the caller to properly wind up the async calls, Commander needs to expose something to allow handling the asynchronous calls.
See #806
Solution
Add a
parseAsync()
call. It calls the normal parse, but then returns a Promise which includes the possibly async action handler. The client can then deal with the Promise in the normal way.It is ok to have a mixture of async and non-async action handlers. Calling parseAsync works with either. Basically, if you have any async action handlers then you should call parseAsync instead of parse.
With this TypeScript code:
See this:
$ npx tsc async.ts && node async.js run+ Reached end of program run- parseAsync success