gpt4 book ai didi

node.js - 使模块异步-nodejs

转载 作者:太空宇宙 更新时间:2023-11-03 23:37:41 25 4
gpt4 key购买 nike

我有以下函数,我将其导出以通过 async.series 方法在其他地方使用。但是,当我在 IDE 的调试器中运行 apiCaller._get 时,它会在执行 get 请求之前返回 undefined。尽管如此,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并在 end 中调用它response 的事件处理程序在 _callback功能。

例如

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 , applybind ,您可以在您选择的上下文中执行回调函数。可能是context提供的对象或您需要的任何其他内容。如果您不需要更改回调执行的 this 绑定(bind),只需使用 TheCallback () 调用它即可。 .

关于node.js - 使模块异步-nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com