从 Node.js 中的回调函数返回一个值 [英] Returning a value from callback function in Node.js
问题描述
我在从 Node.js 中的回调函数返回值时遇到了小麻烦,我将尝试尽可能简单地解释我的情况.考虑我有一个片段,它获取 URL 并点击该 URL 并给出输出:
I am facing small trouble in returning a value from callback function in Node.js, I will try to explain my situation as easy as possible. Consider I have a snippet, which takes URL and hits that url and gives the output:
urllib.request(urlToCall, { wd: 'nodejs' }, function (err, data, response) {
var statusCode = response.statusCode;
finalData = getResponseJson(statusCode, data.toString());
});
我试图将它包装在一个函数中并返回一个这样的值:
I tried to wrap it inside a function and return a value like this:
function doCall(urlToCall) {
urllib.request(urlToCall, { wd: 'nodejs' }, function (err, data, response) {
var statusCode = response.statusCode;
finalData = getResponseJson(statusCode, data.toString());
return finalData;
});
}
因为在我的 Node.js 代码中,我有很多 if-else
语句,其中 urlToCall
的值将被确定,如下所示:
Because in my Node.js code, I have a lot of if-else
statement where value of urlToCall
will be decided, like this:
if(//somecondition) {
urlToCall = //Url1;
} else if(//someother condition) {
urlToCall = //Url2;
} else {
urlToCall = //Url3;
}
事情是urllib.request
中的所有语句都将保持不变,除了urlToCall
的值.所以我肯定需要把这些通用代码放在一个函数中.我尝试了同样的方法,但在 doCall
中总是会返回我 undefined
.我是这样试的:
The thing is all of the statements inside a urllib.request
will remain same, except value of urlToCall
. So definitely I need to put those common code inside a function. I tried the same but in doCall
will always return me undefined
. I tried like this:
response = doCall(urlToCall);
console.log(response) //Prints undefined
但是如果我在 doCall()
中打印值,它会完美打印,但它总是会返回 undefined
.根据我的研究,我开始知道我们不能从回调函数返回值!(是真的吗)?如果是,任何人都可以建议我如何处理这种情况,因为我想防止每个 if-else
块中的重复代码.
But if I print value inside doCall()
it prints perfectly, but it will always return undefined
. As per my research I came to know that we cannot return values from callback functions! (is it true)? If yes, can anyone advice me how to handle this situation, as I want to prevent duplicate code in every if-else
blocks.
推荐答案
Its undefined
因为,console.log(response)
在 doCall(urlToCall) 之前运行;
完成.您还必须传入一个回调函数,该函数在您的 请求 完成时运行.
Its undefined
because, console.log(response)
runs before doCall(urlToCall);
is finished. You have to pass in a callback function aswell, that runs when your request is done.
首先,你的功能.传递一个回调:
First, your function. Pass it a callback:
function doCall(urlToCall, callback) {
urllib.request(urlToCall, { wd: 'nodejs' }, function (err, data, response) {
var statusCode = response.statusCode;
finalData = getResponseJson(statusCode, data.toString());
return callback(finalData);
});
}
现在:
var urlToCall = "http://myUrlToCall";
doCall(urlToCall, function(response){
// Here you have access to your variable
console.log(response);
})
@Rodrigo,在评论中发布了很好的资源.阅读节点中的 回调 及其工作原理.请记住,它是异步代码.
@Rodrigo, posted a good resource in the comments. Read about callbacks in node and how they work. Remember, it is asynchronous code.
这篇关于从 Node.js 中的回调函数返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!