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

parse() works for async action handler in subcommand #1681

Closed
sagargurtu opened this issue Jan 17, 2022 · 1 comment
Closed

parse() works for async action handler in subcommand #1681

sagargurtu opened this issue Jan 17, 2022 · 1 comment

Comments

@sagargurtu
Copy link

Expected Behavior

According to https://github.com/tj/commander.js/blob/master/Readme.md#action-handler, .parseAsync() should be used for async action handlers.

Actual Behavior

If a subcommand uses an async action handler, program.parse() also works.

const makeBuildCommand = () => {
  const program = new Command('build');

  program.argument('<path>').action(async (path) => {
    await new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('Path: ' + path);
        resolve();
      }, 1000);
    });
    console.log('Action Handler completed');
  });

  return program;
};

const init = () => {
  const program = new Command();
  program.addCommand(makeBuildCommand());
  program.parse(process.argv);
};

try {
  init();
} catch (error) {
  console.error(error);
}

Is this expected behavior? Since both .parse() and .parseAsync() work in this case, which variant is recommended?

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jan 17, 2022

Quick answer: if any action handler is async, use .parseAsync().

(It is expected .parse() will sometimes work, with some versions of node, sometimes depending on what else your program is doing or how long it takes. Sometimes.)

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