使模块异步 - nodejs
Make module asynchronous - nodejs
我有以下函数,我导出它以使用 async.series
方法在其他地方使用。但是,当我在 IDE 的调试器中 运行 apiCaller._get
时,它 returns undefined
在执行 get 请求之前。尽管如此,get 请求还是被执行了。但是 `apiCaller._get 不是异步的,我知道它需要回调,但是我不知道在哪里调用回调。
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();
};
传入一个函数作为形式参数TheCallback
,并在_callback
函数中response
的end
事件处理程序中调用它。
例如
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);
});
更新:
通过使用 call
、apply
或 bind
,您可以在您选择的上下文中执行回调函数。它可以是提供的 context
对象或您需要的任何其他对象。如果您不需要为回调执行更改 this 绑定,只需使用 TheCallback ()
.
调用它
我有以下函数,我导出它以使用 async.series
方法在其他地方使用。但是,当我在 IDE 的调试器中 运行 apiCaller._get
时,它 returns undefined
在执行 get 请求之前。尽管如此,get 请求还是被执行了。但是 `apiCaller._get 不是异步的,我知道它需要回调,但是我不知道在哪里调用回调。
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();
};
传入一个函数作为形式参数TheCallback
,并在_callback
函数中response
的end
事件处理程序中调用它。
例如
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);
});
更新:
通过使用 call
、apply
或 bind
,您可以在您选择的上下文中执行回调函数。它可以是提供的 context
对象或您需要的任何其他对象。如果您不需要为回调执行更改 this 绑定,只需使用 TheCallback ()
.