Skip to content

Instantly share code, notes, and snippets.

@auchomage
Last active July 20, 2016 12:08
Show Gist options
  • Save auchomage/ab38853bdb33b2f482978fa5ab2bb90f to your computer and use it in GitHub Desktop.
Save auchomage/ab38853bdb33b2f482978fa5ab2bb90f to your computer and use it in GitHub Desktop.
Referring to a callback
// This file generates output
module.exports = function(callback) {
callback('This is a model answer provided by Andrew Mead.');
};
var strOutput = require('./file1B.js');
strOutput(function(externalInput){
console.log('External Input: ' + externalInput);
});
module.exports = function(callback){
/*
(1) We need to use the request module
(2) We need to also use the url with the api and necessary query parameters
*/
var request = require('request');
var url = 'http://api.openweathermap.org/data/2.5/weather?q=Sutton,uk&appid=a0405ca935c126bca3f178e0230f75b5&units=metric';
console.log('Got weather!');
request({
url: url,
json: true
}, function(error, response, body){
if(error){
callback('Unable to fetch the weather data!') ;
} else {
console.log(JSON.stringify(body, null, 4));
// Print out the name of the city and it's temperature
callback ('In city: ' + body.name + ', ' + body.sys.country + ' the temperature is: ' + body.main.temp + ' deg C.');
}
//The data is contained in the body argument
//4 - is the number of spaces the data has to be indented by
});
}
@andrewjmead
Copy link

Correct. If you're using a function someone else wrote, you're not going to see "callback" in your code.

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