Skip to content
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 method _chainOrCall to return the promise result. #1907

Closed
Rmannn opened this issue Jul 14, 2023 · 3 comments
Closed

Allow method _chainOrCall to return the promise result. #1907

Rmannn opened this issue Jul 14, 2023 · 3 comments

Comments

@Rmannn
Copy link

Rmannn commented Jul 14, 2023

Hi, I am the developper of a nestjs module using commander nestjs-console
Our module is built to partially wrap your module logic into a nestjs module.

We were extracting response from the command until the new version.

commander.js/lib/command.js

Lines 1301 to 1305 in 4ef19fa

if (this.parent) {
actionResult = this._chainOrCall(actionResult, () => {
this.parent.emit(commandEvent, operands, unknown); // legacy
});
}

As you can see here, actionResult will always returns undefined.

Can we add a line to return the result ?

if (this.parent) { 
   actionResult = this._chainOrCall(actionResult, () => { 
     this.parent.emit(commandEvent, operands, unknown); // legacy 
     // new line to allow us extracting the response
     return actionResult
   }); 
 } 

I could help opening a PR if you want.

Thx

@shadowspawn
Copy link
Collaborator

I might be misunderstanding your question, so ask again if this answer does not make sense.

Commander does not expect the action handler to return a response, other than a promise if it is async. You might well think it does based on the actionResult variable name, but that it purely for supporting promises. The actionResult is used to work out whether to chain the next piece of code or to call it directly. A non-promise return value from the action handler is promptly forgotten in the next call to _chainOrCall.

For interest, you can see the action handler response was ignored before support for async was added. This is the PR adding the first version of async support: https://github.com/tj/commander.js/pull/1118/files

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jul 15, 2023

The link to #1118 was more relevant than I expected, it looks like you were relying on that implementation to indirectly get the action response, although _actionResults was only being retained to settle promises.

https://github.com/Pop-Code/nestjs-console/pull/345/files#diff-84b68bce2abfcb25c3328df0286963d7dacc924940adc41923bf4f502a6bc95aR105

Again, the internal variable name could lead to incorrect assumptions. Sorry about that. But the public facing API has the action handler returning void:

action(fn: (...args: any[]) => void | Promise<void>): this;

@Rmannn
Copy link
Author

Rmannn commented Jul 16, 2023

Thanks for your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants