<commit_before># Async Validators

If you have wondered how async validators would work with Strickland, you will be delighted at how simple they are. If a validator returns a `Promise`, then Strickland will return a `Promise` for the validation result. When the validation result promise is resolved, async validators will be resolved.

``` jsx
import validate from 'strickland';

function usernameIsAvailable(username) {
    if (!username) {
        return true;
    }

    return new Promise((resolve) => {
        if (username === 'marty') {
            resolve({
                isValid: false,
                message: `"${username}" is not available`
            });
        }

        resolve(true);
    });
}

validate(usernameIsAvailable, 'marty').then((result) => {
    /*
    result = {
        isValid: false,
        value: 'marty',
        message: '"marty" is not available'
    }
    */
});
```

When validation results are invalid, do not reject the promise. Instead, resolve the promise with a validation result that is not valid. As usual, this can be done by returning `false` or an object with `isValid: false`.

It is your application's responsibility to know if one of your validators could return a `Promise`; if so, then you will need to treat the result from `validate` as a `Promise`. If validation short-circuits before the `Promise` is encountered though, then a regular result will be returned instead of a `Promise. Because of this possibility, it is recommended to wrap `Promise.resolve()` around the validation results and remove any uncertainty.

``` jsx
Promise.resolve(validate(validator, value)).then(handleValidationResult);
```

## Additional Async Features

Strickland has support for additional features related to async validation.

* [Async Validator Arrays and Objects](ValidatorArraysAndObjects.md)
* [Two Stage Sync/Async Validation](TwoStageValidation.md)<commit_msg>chore: fix formatting and update async promise example<commit_after># Async Validators

If you have wondered how async validators would work with Strickland, you will be delighted at how simple they are. If a validator returns a `Promise`, then Strickland will return a `Promise` for the validation result. When the validation result promise is resolved, async validators will be resolved.

``` jsx
import validate from 'strickland';

function usernameIsAvailable(username) {
    if (!username) {
        return true;
    }

    return new Promise((resolve) => {
        if (username === 'marty') {
            resolve({
                isValid: false,
                message: `"${username}" is not available`
            });
        }

        resolve(true);
    });
}

validate(usernameIsAvailable, 'marty').then((result) => {
    /*
    result = {
        isValid: false,
        value: 'marty',
        message: '"marty" is not available'
    }
    */
});
```

When validation results are invalid, do not reject the promise. Instead, resolve the promise with a validation result that is not valid. As usual, this can be done by returning `false` or an object with `isValid: false`.

It is your application's responsibility to know if one of your validators could return a `Promise`; if so, then you will need to treat the result from `validate` as a `Promise`. If validation short-circuits before the `Promise` is encountered though, then a regular result will be returned instead of a `Promise`. Because of this possibility, it is recommended to wrap `Promise.resolve()` around the validation results and remove any uncertainty. This can be achieved by passing `async: true` in the validation context.

``` jsx
validate(validator, value, {async: true}).then(handleValidationResult);
```

## Additional Async Features

Strickland has support for additional features related to async validation.

* [Async Validator Arrays and Objects](ValidatorArraysAndObjects.md)
* [Two Stage Sync/Async Validation](TwoStageValidation.md)
{'fraction_non_alphanumeric': 0.0869896018260208, 'fraction_numerical': 0.0, 'mean_word_length': 3.564814814814815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1263681', 'n_tokens_mistral': 988, 'n_tokens_neox': 957, 'n_words': 460}