Created
May 21, 2014 12:36
-
-
Save cleuton/4ce9912796a2e88bdff3 to your computer and use it in GitHub Desktop.
promise sample
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
/* | |
* GET home page. | |
*/ | |
var Promise = require('promise'); | |
var Request = require('request'); | |
exports.index = function(req, res){ | |
var aprovada = function(httpresponse) { | |
console.log('Aprovada !!!'); | |
res.render('index',{'resposta' : 'OK, tamanho: ' | |
+ httpresponse.body.length}); | |
}; | |
var rejeitada = function(httpresponse) { | |
res.render('index',{'resposta' : 'Erro: ' | |
+ httpresponse.response.statusCode}); | |
}; | |
var mostrar = function(httpresponse) { | |
console.log('Mostrar !!! status: ' | |
+ httpresponse.response.statusCode); | |
return new Promise(function(resolve,reject) { | |
resolve(httpresponse); | |
}); | |
}; | |
var promise = new Promise(function (resolve, reject) { | |
console.log('Entrou no comando da promise '); | |
Request('http://www.google.com', function (err, resp, body) { | |
if (err) reject({"response" : resp, "body" : body}); | |
else resolve({"response" : resp, "body" : body}); | |
}); | |
}) | |
.then(mostrar) | |
.then(aprovada, rejeitada) | |
; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment