Skip to content

Instantly share code, notes, and snippets.

@tomekc
Last active March 15, 2016 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomekc/698b8d68ffd34537fca4 to your computer and use it in GitHub Desktop.
Save tomekc/698b8d68ffd34537fca4 to your computer and use it in GitHub Desktop.
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