I am facing an issue with the order of execution while trying to retrieve values from my WebApi for input validation. It appears that the asynchronous nature of the get operation is causing this discrepancy in execution order.
I believe the asynchronous behavior is the cause, but I'm unsure how to resolve it. Can you offer any assistance?
See the example below for reference:
Code:
if (ano.length == 4 && ano > 1900 && ano < 2100) {
// Checking the allowed range for month and day values
if (dia > 0 && dia <= 31 && mes > 0 && mes <= 12) {
// Verifies months with 30 days
if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30) {
return { 'dataValidation': 'data is invalid.' };
}
// handles February for leap years
if (mes == 2) {
// checks for leap year
if (ano % 4 == 0 && (ano % 100 != 0 || ano % 400 == 0)) {
// Maximum 29 days for February in a leap year
if (dia > 29)
return { 'dataValidation': 'data is invalid.' };
} else if (dia > 28) {
return { 'dataValidation': 'data is invalid.' };
}
}
} else {
return { 'dataValidation': 'data is invalid.' };
}
// 3rd step
this.filiadoService.validarIdadeFiliacao(c.value).subscribe(
data => {
this.dataNascimentoModel = data;
});
//2nd step
if (!this.dataNascimentoModel.IsValido) {
return { 'dataValidation': 'data is invalid.' };
} else {
return null;
}