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
Async API in the same module as the sync one #1487
Comments
Future API usage examplesWith
|
@mleguen are you thinking this replaces |
@bcoe I do no longer see at the moment any use for a separate yargsa module. |
@mleguen @petrgrishin I've been thinking a bit today about the async behavior in yargs and yargsa (which I've come around to thinking was a bad idea.). I agree with @mleguen's original point in #1420, that the library's mixing of sync and async paradigms creates for a confusing experience... I landed promise support in a few places when users submitted patches for the functionality, without thinking too deeply about the inherit complexity it introduced ... unfortunately this complexity is introduced for a feature I suspect is used by a small percentage of yargs' users. Part of why I suggested the Where I Find Myself LandingA synchronous API is fine for argument parsing (which usually takes ms, and happens early in the application's lifecycle) ... we have millions of folks using this library, and don't have an army of people banging on the door asking for the annoyance of calling However... I think @mleguen's suggestions point out the potential for a major improvement to the library:
I think this is getting close to @mleguen's proposal in this issue, and his original suggestions in #1420... The main difference in my thinking, is that I'm not proposing this would ever replace the synchronous version of yargs. Rather, it's additional behavior you opt in to (with additional constraints imposed that make the behavior more logical). Next StepsRather than moving directly to landing code, as proposed in #1488, I suggest we work on a PR that fleshes out the API in |
@bcoe @coreyfarrell If:
then I think that even the
With all these constraints, why not simply adding xxxAsync method to the current API (parseAsync, etc.)? We would then limit duplication or refactoring to these methods, not to the whole API. |
Where I feel it makes sense for yargs to have async functionality is in little niceties like this: const argv = yargs.command('fetch <url>', 'fetch the contents of a URL', () => {}, async (argv) => {
const res = await fetch(argv.url)
console.info(`status = ${res.status} ${res.statusText}`)
console.info(await res.text())
}).argv ☝️ It's nice to be able to provide an I don't think it would be as elegant to have to call
☝️ this pattern of having a separate
If someone runs: const yargs = require('yargs') They get the same API surface they have today, except you can no longer pass in promises to handlers, middleware, etc. If someone runs: const yargs = require('yargs').async You can pass in async middleware, builders, and handlers, but the library resolves a promise on I think this distinction could put us in a position to offer an API that supports both worlds really well. |
Closed in favor of the discussion in #1491. |
As discussed in #1478 , the async API of yargs should be accessible as follows:
which would increase code reuse between the sync and async API, and the project maintenability, compared to the first approaches described in #1420 and yargs/yargsa#4.
Todo:
The text was updated successfully, but these errors were encountered: