Last active
August 29, 2015 14:02
-
-
Save apostololeg/b88890219ce02623a668 to your computer and use it in GitHub Desktop.
vow promises
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
var vow = require('vow'), | |
request = require('request'), | |
handler = 'http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username=demo'; | |
function getData(remote) { | |
if(remote) { | |
var promise = new vow.promise(); | |
request(remote, function(err, req, body) { | |
promise.fulfill(body); | |
}); | |
return promise; | |
} else { | |
return vow.resolve('none'); | |
} | |
} | |
vow.all([ | |
getData(handler), | |
getData(), | |
getData(handler), | |
getData() | |
]) | |
.then(function(data) { | |
console.log('------------', data); | |
}); | |
getData() | |
.then(function(data) { | |
console.log('synchronously', data); | |
}); | |
getData(handler) | |
.then(function(data) { | |
console.log('asynchronously', data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment