Skip to content

Best way to make action async? #505

@AGresvig

Description

@AGresvig

In the pursuit of cleaner and more readable code, I want to leverage ES6 and leave callback hell, so I'm trying to get my .action() callbacks to become async methods, but I haven't yet found the optimal route.

I've tried wrapping the CB with co in two ways:

  1. program.command('myCmd').action(program => co(function* (program) {...}).catch(err => console.log(err.stack)) );
    and
  2. program.command('myCmd').action(co.wrap(function* (program) { .. }));

The problem with 1) is that the program parameter isn't passed

The problem with 2) is that errors are swallowed...

I'd really like to get this working as it yields much nicer code in my use case - involving a lot of http requests and also waiting for user input using the co-prompt library..

Is it a better option altogether perhaps to wrap program.Command.prototype.action somehow?

@tj I know this is a piece of cake for you - pls enlighten me!

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