Last active
August 29, 2015 14:10
-
-
Save victorvhpg/42b0bd468f78c354060e to your computer and use it in GitHub Desktop.
exemplo ajax com promise
This file contains hidden or 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
function ajaxPromise(config) { | |
return new Promise( | |
function(resolve, reject) { | |
var xhr = new XMLHttpRequest(); | |
var p = []; | |
var parametros = null; | |
config.parametros = config.parametros || {}; | |
for (var parametro in config.parametros) { | |
p.push(parametro + "=" + config.parametros[parametro]); | |
} | |
parametros = p.join("&"); | |
if (config.tipoMetodo !== "POST") { | |
config.url += (config.url.indexOf("?") === -1) ? ("?" + parametros) : ("&" + parametros); | |
parametros = null; | |
} | |
xhr.open(config.tipoMetodo, config.url); | |
xhr.setRequestHeader("Content-type", { | |
"POST": "application/x-www-form-urlencoded", | |
"GET": "text/html" | |
}[config.tipoMetodo]); | |
xhr.onreadystatechange = function() { | |
if (this.readyState !== 4) { | |
return; | |
} | |
if (xhr.status === 200) { | |
// sucesso | |
resolve(this.responseText); | |
return; | |
} | |
//ocorreu algum erro no servidor | |
reject(new Error("ocorreu algum erro no servidor: " + this.statusText)); | |
} | |
xhr.onerror = function() { | |
reject(new Error("Erro de ajax")); | |
}; | |
xhr.send(parametros); | |
}); | |
} | |
//======================================================= | |
var fazAlgumaCoisaAsync = function(nome) { | |
return new Promise(function(resolve, reject) { | |
window.setTimeout(function() { | |
resolve("OI " + nome); | |
}, 2000); | |
}); | |
}; | |
//========================================================= | |
var promise = ajaxPromise({ | |
url: "https://graph.facebook.com/victorvhpg", //"https://graph.facebook.com/victorvhpg", | |
tipoMetodo: "GET" | |
}).then(function(valor){ | |
console.log("resposta do ajax: " + valor);// resposta do servidor"{ ....}" | |
return JSON.parse(valor);//parse para JSON | |
}).then(function(valor) { | |
//valor agora ja é um objeto JSON | |
console.log(valor.id); | |
return fazAlgumaCoisaAsync(valor.name); | |
}).then(function(valor) { | |
console.log(valor);// OI Victor Hugo | |
}).catch(function(erro) { | |
//caso algum erro seja no ajax ou no JSON.parse ou no fazAlgumaCoisaAsync | |
console.log(erro); | |
}).then(function(){ | |
//aqui sempre sera executada (menos no caso de o .catch anterior gere um Exception) | |
console.log("SEMPRE SERA EXECUTADO"); | |
}); | |
//========================================================= | |
var promise2 = ajaxPromise({ | |
url: "teste.php", | |
tipoMetodo: "POST", | |
parametros: { | |
mensagem: "oi", | |
teste: 2 | |
} | |
}); | |
//caso sucesso | |
promise2.then(function(valor){ | |
console.log(valor); | |
}); | |
//caso erro | |
promise2.catch(function(erro) { | |
console.log(erro); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment