I'm mad about this: Looking for validation of some fields via AJAX, inputs «identificacion», «usuario» and «correo» can't be duplicated in database.
The function:
async function duplicado(campo) {
let URLscript = window.location.protocol+"//"+window.location.hostname+document.getElementById("ruta_relativa").value;
let opciones = {
method: 'POST',
body: "campo="+campo.name+"&valor="+campo.value,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
};
let peticion = await fetch(URLscript+"comprobar_duplicados.php",opciones);
let respuesta = await peticion.json();
console.log(respuesta.duplicado);
return respuesta.duplicado;
}
document.addEventListener("change", function(evento) {
if ((evento.target.id == "identificacion") || (evento.target.id == "usuario") || (evento.target.id == "correo")) {
console.log(duplicado(evento.target));
if (duplicado(evento.target)) {
alert("Error... duplicated");
evento.target.focus();
}
}
});
Inside function, console logs the right value and I try to return it. Outside function -before it needed- console logs something like this:
Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: <true|false>
How can I get PromiseResult value and use it in a expression? Thanks a lot.
PD: My apoligize: First time in StackOverflow.