简体   繁体   中英

Execution Order

I am trying get the values of my WebApi to validade my input, but the order of execution is not the same that it was encoded.

I think this is because the get is asynchronous, but I don't know how I can resolve it.

Can you help me?

Following example below(image) ->

Code:

if (ano.length == 4 && ano > 1900 && ano < 2100) {
    // Verificando o intervalo permitido para os valores dos meses e dias
    if (dia > 0 && dia <= 31 && mes > 0 && mes <= 12) {
        // Verifica os meses que posuem dia 30 dias  
        if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30) {

            return { 'dataValidation': 'data is invalid.' };
        }
        // caso seja mês 2 verifica se o ano é bissexto
        if (mes == 2) {
            //se for bissexto
            if (ano % 4 == 0 && (ano % 100 != 0 || ano % 400 == 0)) {
                // Se for bissexto pode o dia ser no máximo 29   
                if (dia > 29)
                    return { 'dataValidation': 'data is invalid.' };
                // se não for bisexto o dia pode ser no máximo 28                  
            } else if (dia > 28) {

                return { 'dataValidation': 'data is invalid.' };
            }
        }
    } else {

        return { 'dataValidation': 'data is invalid.' };
    }

    // 3°
    this.filiadoService.validarIdadeFiliacao(c.value).subscribe(
        data => {
            this.dataNascimentoModel = data;
        });

    //2°
    if (!this.dataNascimentoModel.IsValido) {
        return { 'dataValidation': 'data is invalid.' };
    } else {
        return null;
    }

Yes, like you said it's because of the async nature of http calls. This is pretty easy to fix, though! Just move the logic that should come after the http call into the subscribe callback.

But now you can't return anything anymore from within the subscribe callback, that's why you need to specify your own callback as a method parameter where you pass the object instead of returning it (eg callback({ 'dataValidation': 'data is invalid.' }) )

methodName(..., callback: (validationObject) => void) : void {
    if (ano.length == 4 && ano > 1900 && ano < 2100) {
        // Verificando o intervalo permitido para os valores dos meses e dias
        if (dia > 0 && dia <= 31 && mes > 0 && mes <= 12) {
            // Verifica os meses que posuem dia 30 dias  
            if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30) {

                callback({ 'dataValidation': 'data is invalid.' });
                return;
            }
            // caso seja mês 2 verifica se o ano é bissexto
            if (mes == 2) {
                //se for bissexto
                if (ano % 4 == 0 && (ano % 100 != 0 || ano % 400 == 0)) {
                    // Se for bissexto pode o dia ser no máximo 29   
                    if (dia > 29)
                        callback({ 'dataValidation': 'data is invalid.' });
                        return;
                    // se não for bisexto o dia pode ser no máximo 28                  
                } else if (dia > 28) {

                    callback({ 'dataValidation': 'data is invalid.' });
                    return;
                }
            }
        } else {
            callback({ 'dataValidation': 'data is invalid.' });
            return;
        }

        // 3°
        this.filiadoService.validarIdadeFiliacao(c.value).subscribe(
            data => {
                this.dataNascimentoModel = data;

                if (!this.dataNascimentoModel.IsValido) {
                    callback({ 'dataValidation': 'data is invalid.' });
                } else {
                    callback(null);
                }
            }
        );
    }
}

You can call the method like this:

methodName(..., (data) => {
    if (!data) {
        console.log("data is valid");
    }
    else {
        console.log(data.dataValidation);
    }
});

Thank you for you reply! i resolved importing the NG_ASYNC_VALIDATORS and using the Promise.

return new Promise(resolve =>
            this.filiadoService.validarIdadeFiliacao(c.value).subscribe(res => {
                if (res.IsValido == true) {
                    resolve(null);
                }
                else {
                    resolve({ 'dataValidation': 'data is invalid.' });
                }
            }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM