Skip to content

Instantly share code, notes, and snippets.

@cleuton
Created May 21, 2014 12:36
Show Gist options
  • Save cleuton/4ce9912796a2e88bdff3 to your computer and use it in GitHub Desktop.
Save cleuton/4ce9912796a2e88bdff3 to your computer and use it in GitHub Desktop.
promise sample
/*
* 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