Last active
March 15, 2016 11:05
-
-
Save tomekc/698b8d68ffd34537fca4 to your computer and use it in GitHub Desktop.
Learning playground for Bluebird.js
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
// | |
// 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