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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: improve support for async/await #1823

Merged
merged 17 commits into from Jan 11, 2021
Merged

feat!: improve support for async/await #1823

merged 17 commits into from Jan 11, 2021

Conversation

bcoe
Copy link
Member

@bcoe bcoe commented Dec 6, 2020

In node@15 top level await is now supported 馃帀.

This means that we can have yargs return a promise in more situations, with users writing code that looks like this:

import yargs from './index.mjs';
import { setTimeout } from 'timers/promises';

const argv = await yargs(process.argv.slice(2))
  .command(
    'foo',
    'foo commnd',
    () => {},
    async (argv) => {
      console.info('second')
      await setTimeout(1000)
    }
  )
  .middleware(async (argv) => {
    console.info('first')
    await setTimeout(1000)
    argv.value = 99
  }).argv;
console.info('third', argv)

This PR begins work to add better first class support of promises to yargs.


TODO:

BREAKING CHANGES:

  • now returns a promise if handler is async.
  • onFinishCommand removed, in favor of being able to await promise.
  • getCompletion now invokes callback with err and `completions, returns promise of completions.

Fixes #1791, #1420, #1235

@bcoe
Copy link
Member Author

bcoe commented Jan 9, 2021

For anyone who would like to try this and give feedback (which is greatly appreciated):

npm i yargs@next

@bcoe
Copy link
Member Author

bcoe commented Jan 9, 2021

@mleguen @SkeLLLa @petrgrishin @dapplion this PR removes onFinishCommand in favor of a variety of work to make commands work better with promises. You can now simply await yargs when it has an async command.

I would love your feedback. You can install this version of yargs like so:

npm i yargs@next

@dapplion
Copy link
Contributor

dapplion commented Jan 9, 2021

@mleguen @SkeLLLa @petrgrishin @dapplion this PR removes onFinishCommand in favor of a variety of work to make commands work better with promises. You can now simply await yargs when it has an async command.

I would love your feedback. You can install this version of yargs like so:

npm i yargs@next

The overall direction looks great! Can't wait to have this on master

docs/advanced.md Outdated

If you use async middleware or async handlers for commands, `yargs.parse` and
`yargs.argv` will return a Promise. When you `await` this promise the final
parsed result will be returned:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note that this will be returned after the handler has finished.

Copy link

@mdy970 mdy970 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi

@dossy
Copy link

dossy commented May 21, 2021

I was surprised and disappointed by the removal of onFinishCommand().

package.json indicates node >= 12 which should have been changed to node >= 15 if you're going to remove onFinishCommand() in yargs@17.

@jesec
Copy link

jesec commented May 7, 2022

@bcoe The typing is wrong. See my comment in #2175 (comment).

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

Successfully merging this pull request may close these issues.

Default command invoked when calling .showHelp
6 participants