Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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