Created
January 14, 2019 15:52
-
-
Save MarioCares/2926478e21b9dbc7db331e8565d25eb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementsByName('#AlgoQueDispare').addEventListener('evento', function(){ | |
la_funcion_promesa().then(function(algun_valor){ | |
// hago algo con 'serie' que es el valor que me respondió el servidor en la OTRA función | |
console.log(algun_valor); | |
}); | |
}); | |
var la_funcion_promesa = function() { | |
return new Promise(function(resolve, reject) { | |
var xhttp = new XMLHttpRequest(); | |
var data = new FormData(); | |
data.append('dato', 'algun_dato_para_el_servidor'); | |
xhttp.onreadystatechange = function() { | |
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { | |
//resolve es la función que "devuelve" el valor a la función original que levantó la promesa | |
resolve(this.response.lo_que_sea_que_responda_el_servidor); | |
} | |
}; | |
xhttp.onerror = function() { | |
reject(console.log("pifia por algo")); | |
}; | |
xhttp.open('POST', URL_DEL_SERVIDOR, true); | |
xhttp.responseType = 'json'; | |
xhttp.send(data); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment