admin管理员组文章数量:1394759
I have a JavaScript (Node) function to grab the content of a web page and handle it with a callback:
'using strict';
var http = require('http');
function download(url, callback) {
http.get(url, function(res) {
var content = '';
res.on('data', function (chunk) {
content += chunk;
});
res.on('end', function() {
callback(content);
});
}).on('error', function() {
callback(null);
});
}
What I don't understand is why I can't simply return the result on 'end'. Clearly, when the 'end' event is emitted, the 'content' variable contains a string with the content of a web page, otherwise it couldn't be submitted to the callback function, so why can't I just return it like this:
function download2(url) {
http.get(url, function(res) {
var content = '';
res.on('data', function(chunk) {
content += chunk;
});
res.on('end', function() {
return content;
});
}).on('error', function() {
return null;
});
}
download2 always returns undefined. Why?
I have a JavaScript (Node) function to grab the content of a web page and handle it with a callback:
'using strict';
var http = require('http');
function download(url, callback) {
http.get(url, function(res) {
var content = '';
res.on('data', function (chunk) {
content += chunk;
});
res.on('end', function() {
callback(content);
});
}).on('error', function() {
callback(null);
});
}
What I don't understand is why I can't simply return the result on 'end'. Clearly, when the 'end' event is emitted, the 'content' variable contains a string with the content of a web page, otherwise it couldn't be submitted to the callback function, so why can't I just return it like this:
function download2(url) {
http.get(url, function(res) {
var content = '';
res.on('data', function(chunk) {
content += chunk;
});
res.on('end', function() {
return content;
});
}).on('error', function() {
return null;
});
}
download2 always returns undefined. Why?
Share Improve this question asked Apr 24, 2014 at 0:39 SuugakuSuugaku 2,7475 gold badges32 silver badges33 bronze badges1 Answer
Reset to default 6These are asynchronous functions. They have already long since pleted before the callback functions are called. Thus the desired return result is not known when either of your download functions returns. For data passed to asynchronous callbacks, the ONLY place you can do something with that data is from the callback itself. You can put your code to handle that data in the callback or you can call some other function from within the callback and pass the data to it.
This is asynchronous programming and you really have to get used to it in node because it's used there a lot. It is significantly different than synchronous programming in that you can't call a function that starts an asynchronous operation and expect the parent function to get the result and return it. The result of the asynchronous operation won't be known until sometime later, long after the parent function has already returned.
Thus, the way you've structured it in your first download()
function is the usual way of handling this.
本文标签: nodejsJavaScript callback vs returnStack Overflow
版权声明:本文标题:node.js - JavaScript callback vs return - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744103505a2590968.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论