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
});
}
@auchomage
Copy link
Author

Hi Andrew, another question that springs to mind is this:
In the previous discussion on callbacks, you mentioned that it is important to define the callback and refer to it to be able to use it.
I believe that 'file1.js' - defines the callback in line 3.
'file1B.js' - specifies the input required in line 3 (called 'externalInput') and calls it in line 4.
That I understand.

My question is for file 'weather.js', the parameters defined for the 'request()' do not refer to anything called 'callback'. I understand the parameters to be:

  1. an object {},
  2. anonymous function() that requires 3 parameters (error, response, body).

'callback' isn't mentioned as an input in this anonymous function. Yet it works?
What is the golden rule to understand about naming callbacks. I just about understood the first example and this one now puzzles me. Thanks.

@andrewjmead
Copy link

I believe that 'file1.js' - defines the callback in line 3.

On line three we call the callback, the actual anonymous function is defined in file1B.js.

file1B.js' - specifies the input required in line 3 (called 'externalInput') and calls it in line 4.

I believe you do understand what's happening here, it just a different in terminology. We're not calling anything on line 4. We're "referencing" the string variable. "Calling" implies your executing a funciton.

The reason you don't see "callback" anywhere in weather.js is because our callback function is executed by the request library itself. If we were creating the request() function, we would design it to expect two arguments. We would probably call the first one options and the second one callback.

@auchomage
Copy link
Author

Thanks Andrew. I understand the explanation, but how do I make sense of that in future. If I see 'require ('request') ', I should simply think the request library is accessing the callback function. That is a bit hazy, but is a definite improvement on where I was with this. Many thanks.

@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