Strickland
  • Readme
  • Introduction
    • Validators
    • Validation
    • Validation Results
  • Extensibility
    • Validator Factories
    • Validation Context
    • Validation Result Props
    • Extensibility Pattern
    • formatResult
  • Built-In Validators
    • required
    • compare
    • min
    • max
    • range
    • minLength
    • maxLength
    • length
  • Composition
    • Arrays of Validators
      • every
      • all
      • some
    • Validating Array Elements
      • arrayElements
    • Validating Objects
      • objectProps
      • Advanced Object Validation
      • Nested Objects
      • Arrays of Objects
    • Composition Conventions
    • Composition and formatResult
  • Async Validation
    • Resolving Async Validation
    • Deferred Async Validation
    • Async Validator Arrays and Objects
    • Two-Stage Sync/Async Validation
    • Race Conditions
    • Automatic Race Condition Handling
    • Async Validation and formatResult
  • Form Validation
    • form
    • Async Form Validation
    • validateFields
    • emptyResults
    • updateFieldResults
  • Inspiration
  • Design Goals
  • Wrap-Up
  • Change Log
  • NPM
  • GitHub
Powered by GitBook
On this page

Was this helpful?

  1. Async Validation

Race Conditions

A common pitfall with async validation is to ensure the value hasn't changed during async validation. Fortunately, every validation result from Strickland includes the value that was validated as a result prop, making these race conditions possible to detect and guard against.

Let's take a look at handling this race condition in application code:

const usernameValidator = [
    required(),
    length(2, 20),
    usernameIsAvailableTwoStage
];

let username = 'marty';
let usernameResult = validate(usernameValidator, username);

username = 'mcfly';

if (usernameResult.validateAsync) {
    usernameResult.validateAsync().then((asyncResult) => {
        if (asyncResult.value === username) {
            // this will not be reached since
            // the username has changed
            usernameResult = asyncResult;
        }
    });
}
PreviousTwo-Stage Sync/Async ValidationNextAutomatic Race Condition Handling

Last updated 4 years ago

Was this helpful?