Strickland
Search…
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:
1
const usernameValidator = [
2
required(),
3
length(2, 20),
4
usernameIsAvailableTwoStage
5
];
6
7
let username = 'marty';
8
let usernameResult = validate(usernameValidator, username);
9
10
username = 'mcfly';
11
12
if (usernameResult.validateAsync) {
13
usernameResult.validateAsync().then((asyncResult) => {
14
if (asyncResult.value === username) {
15
// this will not be reached since
16
// the username has changed
17
usernameResult = asyncResult;
18
}
19
});
20
}
Copied!
Last modified 1yr ago
Copy link