Javascript function always returns Undefined

Asked

Viewed 927 times

0

I know there are several similar questions, but none of the solutions presented worked :(

I have this function which receives data per parameter:

function funcao(valor) {
    var a;
    $.getJSON(/* aqui retorno de uma WebService*/, function (dados) {
        if ("error" in dados) {
            a = 0; //seria o true e false, mas não funcionaram também
            return a;
        } else {
            /* se o WS está ok faz algo e então seria true */
            a = 1;
            return a;
        }
    });
}

I tried to treat Return with

return true;
return 1;
return "true";
return 'true';
return a.val();
return a.value;
return a;
return !!a;

this in turn is used as follows:

function valida(){
    if(funcao(dados)){
        faz algo
    } else if(funcao(dados)){
        algo
    }else{
        algo
    }
}

have already tried to treat the if com

if(func(dados)){}
if(func(dados) == true){}
if(func(dados) === true){}
if(func(dados) == 1){}
if(func(dados) === 1){}
if(func(dados) === "1"){} //a parte de == ou === tentei também e vale para os demais
if(func(dados) === "true"){}
if(func(dados) === 'true'){}
if(func(dados).val()){}
if(func(dados).value){}
if(func(dados).value === true){}
if(func(dados).val() === true){}

all these attempts returned value Undefined

I’ve read many questions from here in the stack and elsewhere, but all the solution possibilities didn’t work.

I appreciate the help!

  • The function funcao(dados) {} that is returning "Undefined"?

1 answer

1

It doesn’t work this way because Ajax is asynchronous, i.e., when you call the function that in turn calls the other function expecting to receive an Ajax value, it immediately returns Undefined because the return of Ajax is not immediate.

What you can do is use .then that processes the code after the return of Ajax. It is also necessary to pass a parameter in the function valida(dados), where dados is the value that will be sent to the function funcao(valor).

See how it would look:

function valida(dados){

   funcao(dados).then(function(retorno){
      if(retorno){
         faz algo
      } else if(!retorno){
         faz algo
      }else{
         faz algo
      }
   });

}

function funcao(valor) {
    return $.getJSON(/* aqui retorno de uma WebService*/).then(function (dados) {
        if ("error" in dados) {
            return false;
        } else {
            /* se o WS está ok faz algo e então seria true */
            return true;
        }

    });
}

valida("algum valor"); // aqui chama a função
  • Then . then is used after the function I must "wait", it makes me work synchronously, in the places I declare. Ali on getJSON() you put . then after get, it makes a difference? pq after . then the data who would have in the variable "data" would not be the return of it and not the return of JSON (as expected)?

  • The "data" variable in get is the json that will come back in ajax.

  • If there is any "error" key, it will return "false"

  • "Then" after the get eh callback, otherwise the code does not wait for the return.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.