Skip to content

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

@sagargurtu

Description

@sagargurtu

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions