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

Handle sync validations #2053

Merged
merged 5 commits into from
Jul 28, 2023

Conversation

DaddyWarbucks
Copy link
Contributor

Solves: #2052

Comment on lines 145 to 150
if (!options.sync) {
return Promise.resolve(result).then(
handleResult,
handleError,
);
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if (!options.sync) {
return Promise.resolve(result).then(
handleResult,
handleError,
);
}
if (options.sync)
throw Error(...)
return result.then(
handleResult,
handleError,
);

I think this is all you actually need here? the sync check now only serves to throw if there is a promise when there shouldn't be

Copy link
Owner

@jquense jquense left a comment

Choose a reason for hiding this comment

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

SGTM, trying to think if there are potential race conditions now...there shouldn't be, or at least if their are they also affect the explicit sync pathway as well.

this is a little Zalgo-y but i think it's probably worth it...

@DaddyWarbucks
Copy link
Contributor Author

DaddyWarbucks commented Jul 28, 2023

@jquense I spun up this little repo as a test bed: https://github.com/DaddyWarbucks/yup-sync-validation

I roughly estimated the schema to have about 100 validation tests per data from the repo. Given every property has multiple tests, I just rounded to 100 for the statements below.

validateSync.loop and validateAsync.loop are interesting because they have similar performance. On every iteration of the validateAsync.loop 100 promises are pushed/popped off the event loop at a time. So the event loop doesn't cough on that.

The problem seems to present itself when using promise.all and promise.allSettled because the event loop is overloaded with 1 million+ promises at once. The new approach improves that by only applying true async tests to the event loop.

@jquense jquense merged commit b4627a2 into jquense:master Jul 28, 2023
1 check passed
@DaddyWarbucks DaddyWarbucks deleted the bug/2052-validate-promises branch August 5, 2023 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants