Skip to content

Instantly share code, notes, and snippets.

@tomekc tomekc/learn_bluebird.js
Last active Mar 15, 2016

Embed
What would you like to do?
Learning playground for Bluebird.js
//
// npm install bluebird
// npm install superagent
//
var Promise = require('bluebird');
var request = require('superagent');
console.log('Promises with Bluebird');
function get(url) {
var p = new Promise(function(resolve, reject) {
request.get(url)
.end(function(err, res) {
if (err) {
reject(err);
} else {
resolve(res.body);
}
});
});
return p;
}
var URL = 'http://httpbin.org';
var lista = [get(URL + '/ip'), get(URL + '/user-agent')];
Promise.all(lista)
.spread(function(ip, agent) {
console.log('IP', ip, 'AGENT', agent);
return {
ip: ip,
agent: agent
};
})
// .catch(function(err) {
// return { 'foo' : 'bar'}
// })
.then(function(obj) {
console.log('Info', obj);
})
.catch(function(err) {
console.log('ERROR ', err);
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.