使模块asynchronous – nodejs

我有以下function,我导出到其他地方使用async.series方法。 但是,当我在IDE的debugging器中运行apiCaller._get时,它在执行get请求之前返回undefined 。 获取请求被执行。 但是`apiCaller._get不是asynchronous的,我知道它需要callback,但我不明白在哪里调用callback。

 var http = require("http"); var querystring = require("querystring"); var _ = require("underscore"); apiCaller = {}; apiCaller.token = null; var server=http.createServer(function(req,res){}); server.listen(8080); apiCaller._get = function (context, config, TheCallback) { // get the parameters for our querytring var oauthParams = _.pick(config, "client_id", "client_secret", "grant_type"); // create the querystring var params = querystring.stringify(oauthParams); var options = { method: "GET", hostname: config.host, path: "/my/path/to/token?" + params, headers : { 'Content-Type': "application/json", 'Accept': "application/json" } }; var _callback = function(response) { console.log('STATUS: ' + response.statusCode); console.log('HEADERS: ' + JSON.stringify(response.headers)); var str = ''; //another chunk of data has been recieved, so append it to `str` response.on('data', function (chunk) { str += chunk; }); // error response response.on("error", function (error) { if ( !context ) { console.error("Something went wrong with the api response."); return; } context.done(new Error("Something went wrong with the api response.")); }); //the whole response has been recieved, so we just print it out here response.on('end', function () { apiCaller.token = JSON.parse(str).access_token; // we want to stop the request if token is not correct if ( !apiCaller.token || apiCaller.token === undefined || apiCaller.token === null ) { if ( !context ) { console.error("Something went wrong with the token. Wrong token! Token: %s", apiCaller.token); return; } console.error("Token: %s", apiCaller.token); context.done(new Error("Something went wrong with the token. Wrong token!")); } console.log(str); console.log(apiCaller.token); }); }; var request = http.request(options, _callback); request.on('error', function(e) { console.log('problem with request'); }); request.end(); }; 

传递函数作为forms参数TheCallback并在_callback函数中的responseend事件处理函数中调用它。

例如

 var _callback = function(response) { console.log('STATUS: ' + response.statusCode); console.log('HEADERS: ' + JSON.stringify(response.headers)); var str = ''; //another chunk of data has been recieved, so append it to `str` response.on('data', function (chunk) { str += chunk; }); // error response response.on("error", function (error) { if ( !context ) { console.error("Something went wrong with the api response."); return; } context.done(new Error("Something went wrong with the api response.")); }); //the whole response has been recieved, so we just print it out here response.on('end', function () { apiCaller.token = JSON.parse(str).access_token; // we want to stop the request if token is not correct if ( !apiCaller.token || apiCaller.token === undefined || apiCaller.token === null ) { if ( !context ) { console.error("Something went wrong with the token. Wrong token! Token: %s", apiCaller.token); return; } console.error("Token: %s", apiCaller.token); context.done(new Error("Something went wrong with the token. Wrong token!")); } console.log(str); console.log(apiCaller.token); TheCallback.apply(context, arguments); }); 

更新:

通过使用callapplybind ,您可以在您select的上下文中执行callback函数。 它可以是context对象提供或任何你需要的东西。 如果您不需要为callback执行更改此绑定,则只需使用TheCallback ()即可调用它。