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

Add a promised based handle() #215

Merged

Conversation

voxpelli
Copy link
Contributor

@voxpelli voxpelli commented Jan 9, 2021

Nowadays my feeling is that promise/async/await is pretty much the standard way of doing asynchronous development in node.js, so in my own projects and the projects I did at @Sydsvenskan I use a wrapper that makes .handle() be promised based.

This is that wrapper upstreamed to be part of handle() itself, making the callbacks argument optional and when it's omitted, instead a promise is returned.

@voxpelli
Copy link
Contributor Author

voxpelli commented Jan 9, 2021

If this project should stick with es3, then I need to remove the arrow functions here. Maybe could be okay to start moving towards a new major version where things like arrow functions and const / let is allowed? The benefit of sticking to es3 is pretty slim nowadays? (I'm saying that as someone who stuck with es3 for quite some time)

Copy link
Collaborator

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

This looks great :-)

We'll need some tests for this functionality as well.

(There's not enough benefit in using the newer syntax versus the users it excludes, so let's continue using ES3 here)

are there other callback-taking APIs in this library that should get the same treatment? It'd be ideal to update them all at once.

lib/forms.js Show resolved Hide resolved
lib/forms.js Outdated Show resolved Hide resolved
@voxpelli
Copy link
Contributor Author

are there other callback-taking APIs in this library that should get the same treatment? It'd be ideal to update them all at once.

The only other ones would be the form.validate(callback) and field.validate(callback) pair.

Possibly one would then also allow validator methods to be Promises as they currently instead deliver the result through a (errMessage = undefined) => undefined callback. One could check whether the validator function instead returns a Promise and if it does, then one could use whatever it resolves to instead. (If one did that, then I would also suggest having such a promise reesolve to something like { err: true, message: 'Something'} rather than having the error status be implicitly defined by the existence of an error message)

Maybe fix form.validate(callback) and field.validate(callback) in this PR and do a follow up for possibly accepting promised validators?

@voxpelli voxpelli requested a review from ljharb January 19, 2021 18:29
@voxpelli
Copy link
Contributor Author

Right, tests, I'll add, sorry for premature review request. Coming soon!

@voxpelli
Copy link
Contributor Author

@ljharb There, fixed

In regards to forms.validate(): It uses the async module to do its validation, I'm wondering if it maybe would be sensible to add a polyfill for Promise and port that over to use Promise internally and depending on whether a callback is given, either return that Promise or call the callback with the result of the Promise.

As such I think a new issue / PR for that would make sense? Either one of us create an issue and then I can make a PR

Copy link
Collaborator

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

LGTM! i'll rebase with some changes.

test/test-form.js Outdated Show resolved Hide resolved
test/test-form.js Outdated Show resolved Hide resolved
test/test-form.js Outdated Show resolved Hide resolved
test/test-form.js Outdated Show resolved Hide resolved
@ljharb ljharb force-pushed the feature/optional-promise-based-handle branch from 1283079 to f26c8cd Compare January 19, 2021 19:18
@ljharb
Copy link
Collaborator

ljharb commented Jan 19, 2021

@voxpelli it's not a good idea to ship a Promise polyfill - especially since most people either won't need one, or will already be shipping their own. We can definitely look into validate in a separate PR, though.

@ljharb ljharb force-pushed the feature/optional-promise-based-handle branch from f26c8cd to 11200d8 Compare January 19, 2021 19:23
@codecov-io
Copy link

codecov-io commented Jan 19, 2021

Codecov Report

Merging #215 (11200d8) into master (4b99dfa) will increase coverage by 0.32%.
The diff coverage is 90.90%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #215      +/-   ##
==========================================
+ Coverage   54.86%   55.19%   +0.32%     
==========================================
  Files          12       12              
  Lines        1059     1069      +10     
  Branches      259      261       +2     
==========================================
+ Hits          581      590       +9     
- Misses        478      479       +1     
Impacted Files Coverage Δ
lib/forms.js 89.65% <90.90%> (+0.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b99dfa...11200d8. Read the comment docs.

@voxpelli
Copy link
Contributor Author

@voxpelli it's not a good idea to ship a Promise polyfill - especially since most people either won't need one, or will already be shipping their own. We can definitely look into validate in a separate PR, though.

I was thinking a ponyfill that would only be used when no global Promise is available, and justify that by the fact that the async module is probably as big as such a ponyfill, but is included for all users today, also those with Promises support.

Should I create an issue for it then?

@ljharb
Copy link
Collaborator

ljharb commented Jan 19, 2021

Sure, we can discuss it there.

@ljharb ljharb merged commit 11200d8 into caolan:master Jan 19, 2021
@voxpelli voxpelli deleted the feature/optional-promise-based-handle branch March 13, 2024 18:51
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.

None yet

3 participants