final-form icon indicating copy to clipboard operation
final-form copied to clipboard

Pending promises are not getting flushed when we use getValidator method of Field

Open akshaykhanna opened this issue 6 years ago • 1 comments

Are you submitting a bug report or a feature request?

Bug report

What is the current behaviour?

We are facing this issue when we trigger validations (using getValidator) on typing each character in the text field.

  1. Whenever we type each character in the text field, we are calling getValidator method which in turn creates new promise for each call. image

  2. Inside each Promise call, we are wrapping our functionality inside debounce & due to which some promises are not getting settled ( stuck in the pending state). This is because debounce works in a way like it only resolve the last passed function when its timeout has elapsed. This issue is prominent in our codesandbox example when we type multiple characters too fast (not giving enough time to the previous promise to get resolved)

  3. Hence whenever we click on the submit button (try submitting the form), we are unable to proceed, because some promise (which were ignored by debounce) are still stuck in the pending state.

  4. On clicking submit, final form submit method returns in between if some promises are yet to be settled.

var asyncValidationPromisesKeys = Object.keys(asyncValidationPromises);
 if (asyncValidationPromisesKeys.length) {
       // still waiting on async validation to complete...
       Promise.all(asyncValidationPromisesKeys.map(function (key) {
         return asyncValidationPromises[Number(key)];
       })).then(api.submit, console.error);
       return;
     }

image

What is the expected behaviour?

getValidator should only cater to the last promise & not the previous ones. Or final-form should not return in between if pending promises are there in the queue (provided the last promise is resolved)

Sandbox Link

https://codesandbox.io/s/atlaskit-example-template-23ftq?file=/src/validatedForm.js Steps to reproduce in this example:-

  1. Try typing fast in the first textbox & then clicking on the alert button which will not work.
  2. Try the same with second text box & it will work as we have manually flushed old promises in it in debounceFetch.js

What's your environment?

final-form ver: 4.20.0, OS: Mac 10.13.6 & Browser: Chrome: 83.0.4103.97

akshaykhanna avatar Jun 10 '20 11:06 akshaykhanna

Investigation Results

I've investigated this issue and here's what I found:

Root Cause:

When using debounced async validators, the debounce function doesn't reject old promises - it simply doesn't call them. This leaves those promises in a pending state forever. When you try to submit, Final Form waits for ALL pending async validation promises to resolve before proceeding, which blocks submission indefinitely.

Recent Related Work:

PR #513 was merged in February 2026 to add per-field async validation tracking (asyncValidationCount and asyncValidationKey). This addressed race conditions but did not solve the debounce/pending promise issue.

Why This is Tricky:

The fundamental issue is that Final Form has no way to know which promises have been "abandoned" by debounce. The promises are created and tracked, but debounce silently decides not to resolve/reject them.

Workarounds:

  1. Your solution (recommended): Manually reject old promises when debouncing, as shown in your debounceFetch.js example. This is the cleanest approach.

  2. Alternative: Use a promise wrapper that auto-rejects after a timeout:

const withTimeout = (promise, ms) => 
  Promise.race([
    promise,
    new Promise((_, reject) => 
      setTimeout(() => reject(new Error('Validation timeout')), ms)
    )
  ])

Potential Fix:

Final Form could track promises per-field (using field name as key instead of global counter) and cancel old promises when a new one starts for the same field. However, this would be a breaking change since it changes validation behavior.

Recommendation:

This is a user implementation issue, not a Final Form bug. The library cannot know your debounce strategy. The solution is to ensure your debounced validators properly reject abandoned promises.

I recommend closing this issue as "working as intended" with documentation suggesting promise cancellation patterns for debounced validators.

If you'd like, I can prepare a PR adding a section to the docs about async validation best practices with debouncing.

erikras-richard-agent avatar Feb 17 '26 00:02 erikras-richard-agent