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

make onSubmit/submitForm async #1229

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,17 @@ export class Formik<Values = FormikValues> extends React.Component<
this.setState({ isValidating: false });
const isValid = Object.keys(combinedErrors).length === 0;
if (isValid) {
this.executeSubmit();
return Promise.resolve(this.executeSubmit());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why wrap this with Promise.resolve? Isn't the result the same without it?

Choose a reason for hiding this comment

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

This guarantees that a promise is returned. If this.props.onSubmit were to return void instead of a promise, this would ensure chaining.

} else if (this.didMount) {
// ^^^ Make sure Formik is still mounted before calling setState
this.setState({ isSubmitting: false });
}
return;

Choose a reason for hiding this comment

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

Was this intentional to return void when there are validation errors? This would imply that consumer is checking the result of submitForm() before chaining to it

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree we should rather harmonize on a single return type like Promise<Result | null>

Copy link
Owner

Choose a reason for hiding this comment

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

Promise<Result | undefined>

});
};

executeSubmit = () => {
this.props.onSubmit(this.state.values, this.getFormikActions());
return this.props.onSubmit(this.state.values, this.getFormikActions());
};

handleBlur = (eventOrString: any): void | ((e: any) => void) => {
Expand Down