First of all thanks to this community. This is my first query.
I have a javascript function that makes an ajax call to a script. Inside this function, the value of the variables is correct. This is the function (I emphasize that it is the same as many other calls with different "data" parameters):
function existe_dni(dni_a_verificar)
{
$.ajax({
type:'POST',
url:'validacion.php',
data:'dni_existe='+dni_a_verificar,
success:function(html){
var validacion = $.parseJSON(html);
console.log("DNI: " + dni_a_verificar + " (" + typeof(dni_a_verificar) + ")");
console.log("Validacion: " + validacion + " (" + typeof(validacion) + ")");
console.log("");
return validacion;
},
error:function(jqxhr, status, exception){
return -1;
}
});
}
And this is the function that receives the previous return. Here, the variable exists_el_dni always and at all times has the value undefined:
else {
var existe_el_dni = existe_dni(dni);
console.log("(if) DNI: " + dni + " (" + typeof(dni) + ")");
console.log("(if) Existe? " + existe_el_dni);
if (existe_el_dni == -1)
{
dni_ctl.style.borderColor = "red";
errores_personal_cnt++;
errores_personal += "\t- el D.N.I. ya existe\n";
}
}
I really don't know what is not working, since as I said, this function to many others. The console.logs of the first and second functions show the correct values for the dni variable.
From already thank you very much!
If you want to use the data from the call
ajax
you should do it withPromise
instead of the classicsuccess/error
.And it is that it
ajax
returns onePromise
that you can wait for it to be resolved and propagate.Here the call is made to
ajax
. Without the propertiessuccess
norerror
, since the answers will be expected from outside.This function calls the previous
ajaxCall
. Inside the functionthen
will be the response without errors. Thecatch
will be invoked in case the call had some kind of error.This function
existe_dni
will also return aPromise
. The result of this will be the variablevalidacion
in case the callajax
was successful. On error, it just raises the exception up.Finally,
Here again, we will call
existe_dni
but subscribe to the response. The response will obviously be the variablevalidación
in case it went well, and the error will be obtained within the functioncatch
.This can be refactored in a thousand more elegant ways. But I think this, to begin with, is an easy way to understand it.
Edit
With use of
async/await
it would look like this.The call to
existe_dni
would remain the same, with the methodsthen
andcatch
.Edit II
The subtle difference is in how you treat the response and what you do with it. Let us suppose the following case.
This call makes a request for
ajax
and all it does is print the response once it gets it, or print the error, if any.If we call this function
Print the result of the request some time later. Suppose the result is the following
What if we expect you to return something to us?
The function returns
undefined
(well, it's not strange because we don't have anyreturn
in the functionajaxCall
). But we see that first the has returnedundefined
and then the has been calledconsole.log
with the answer.We are going to put a
return
to the function once we have the data.And it returns exactly the same thing, a
undefined
.The
success
object property that we pass as an argument to the functionajax
is a function, a callback . This function will be executed onceajax
you have finished your work.Important, again, is a function . So the
return
inside of this function affects the function itself, not the functionajaxCall
.The function has no
ajaxCall
explicitreturn
.With the following example,
... this is obvious. The
return
withininnerFn
affectsinnerFn
, notfn
.When a function doesn't have a
return
, Javascript interprets it as anreturn undefined;
implicit.fn1
andfn2
they are exactly the same.In short, why is one call different
ajax
from another?It will depend on whether or not that function returns values. If everything you do with the response of
ajax
is done in the response itselfsuccess
, you don't need to implement any pattern ofPromise
(although implicitlyajax
you do it inside).If you need that function to return something, as was your case, you must implement
Promise
so that from the outside you can also wait for that response.So that nothing is left untied, to emulate the calls
ajax
I have used the following.I consider the question answered. If you have other doubts or errors, I suggest you write a different question.
I also encourage you to read the documentation and some interesting articles about asynchronicity in Javascript.
Promise
Promise
I hope it works.