Last active
July 20, 2016 12:08
-
-
Save auchomage/ab38853bdb33b2f482978fa5ab2bb90f to your computer and use it in GitHub Desktop.
Referring to a callback
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
// This file generates output | |
module.exports = function(callback) { | |
callback('This is a model answer provided by Andrew Mead.'); | |
}; |
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 strOutput = require('./file1B.js'); | |
strOutput(function(externalInput){ | |
console.log('External Input: ' + externalInput); | |
}); |
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
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 | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Correct. If you're using a function someone else wrote, you're not going to see "callback" in your code.