Ensure React useAsync calls createPromise once at mount#4013
Merged
Conversation
**Why**: Conflicts with Prettier opinionated formatting
**Why**: The `read` function returned by `useAsync` is expected to be called multiple times, but it is intended to operate on a single promise created once at mount time (or at least once per unique set of arguments). Prior to these changes, this was behaving wrongly by creating a new promise each time `read` was called. While this did not prove to be troublesome for the stubbed upload behavior, in real-world testing it showed to trigger repeated API requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why: The
readfunction returned byuseAsyncis expected to be called multiple times, but it is intended to operate on a single promise created once at mount time (or at least once per unique set of arguments). Prior to these changes, this was behaving wrongly by creating a new promise each timereadwas called. While this was not particularly troublesome for the stubbed upload behavior, in real-world testing it showed to trigger repeated API requests.Included in these changes are improvements to the test suite to try to provide stronger guarantees that both uploading and
useAsync's default behavior will not make repeated calls to create a new promise.Implementation Notes:
This is a pretty unfortunate oversight on my part, and shows that there's a fair bit of complexity in this implementation. It raises a question as to whether the form submission should occur via some more direct means: Calling
uploadat the time ofDocumentCapture'sonCompletehandling, rather than managing this as a combination of component state and lifecycle side effects. That being said, there would still be complexity in managing the more "direct" path, since it needs to handle many states (pre-submission, mid-submission, post-submission success, and post-submission failure) that would likely need to be absorbed intoDocumentCapture. The intent of theSubmissioncomponent anduseAsynchook was to try to encapsulate this, and to isolate the responsibilities of each more appropriately.