Two-Stage Sync/Async Validation
function usernameIsAvailableTwoStage(username) {
if (!username) {
// Do not check availability of an empty username
// Return just a boolean - it will be
// converted to a valid result
return true;
}
// Return an initial result indicating the value is
// not (yet) valid, but availability will be checked
return {
isValid: false,
message: `Checking availability of "${username}"...`,
validateAsync() {
return new Promise((resolve) => {
if (username === 'marty') {
resolve({
isValid: false,
message: `"${username}" is not available`
});
} else {
resolve({
isValid: true,
message: `"${username}" is available`
});
}
});
}
};
}Last updated