Skip to content

Instantly share code, notes, and snippets.

@aleybe
Last active June 1, 2019 18:34
Show Gist options
  • Save aleybe/94017c503f2db8403267afedd814d326 to your computer and use it in GitHub Desktop.
Save aleybe/94017c503f2db8403267afedd814d326 to your computer and use it in GitHub Desktop.
node_getBeerDescriptions
function getBeerApiGET (callback) {
var request = require('request');
request('https://api.punkapi.com/v2/beers/random', function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(body); // Print the google web page.
}
});
};
var getBeer = new Promise(function(resolve, reject) {
getBeerApiGET(function cb(val) {
resolve(val);
});
});
function getBeerOldSchool(callback) {
var https = require('https');
https.get(`https://api.punkapi.com/v2/beers/random`, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
callback(data);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
oldPromiseOldBeer = new Promise(function(resolve, reject) {
getBeerOldSchool(function cb(val) {
resolve(val);
});
});
oldPromiseOldBeer.then(result => {
let n = JSON.parse(result);
console.log('old school : ' + n[0].description);
})
getBeer.then(result => {
let n = JSON.parse(result);
console.log('new school : ' + n[0].description);
});
@aleybe
Copy link
Author

aleybe commented Jun 1, 2019

Example output :
PS C:\Users\alex\Documents\code\node\practice\weekone> node .\main.js
old school : An exclusive barrel-aged blend, brought together to reward investors on Equity for Punks USA; two imperial stouts aged in grain whisky barrels for over a year.
new school : Cloudberry, lingonberry, blueberry and sea buckthorn meld with citrusy hops in an intensely sweet, fruity and floral aroma, with toasty malt notes and tropical fruit bursts. This bitter, resinous IPA follows up with a lemony punch in the mouth - cherry and blueberry sourness takes this pithy pale ale in a new direction before a long, dry finish.
PS C:\Users\alex\Documents\code\node\practice\weekone>
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment