私は簡単な例を作成して、私はdubt持っている:コールバック関数 - NodeJS
var request = require("request");
var url = "http://api.openweathermap.org/data/2.5/weather?q=turin&APPID=xxxxxxxxxxxxxxxxxxxxxx";
module.exports = function (callback) {
request(
{
url: url,
json: true
}, function (error, response, body) {
if (error) {
callback("Unable to fetch weather"); // callback function
} else {
callback("It is " + body.main.temp + " in " + body.name);
}
});
console.log("After request");
};
、外部ファイルからの、私はこのモジュールを必要:この場合
var weather = require("./weather.js");
weather(function (currentWeather) {
console.log(currentWeather);
});
、私はweather
を呼び出しますモジュールと私はcallback
関数(それは天気モジュールの引数です)コマンドラインにトリノの天気を印刷する。しかし、それはどのように仕事ですか?
あなたが求めていることは明確ではありません。 'weather.js'のエクスポートは関数です。その関数をコールバック関数のパラメータで呼び出します。 –
「どうやってできますか?」彼の本当の疑問は「それはどういう仕事ですか?」と思います。 –