I'm using Node.JS to iterate through some data and push that data to an array. However, console.log does not seem to show any changes that I've made. I'd like to be able to process the data in twitter_ids after the function is done pushing data to it.
我正在使用Node.JS迭代一些数据并将数据推送到数组。但是,console.log似乎没有显示我所做的任何更改。我希望能够在完成功能推送数据后在twitter_ids中处理数据。
I'm wondering if it's due to a misunderstanding of Node.JS's asynchronous nature?
我想知道是否是由于对Node.JS的异步性质的误解?
var twitter_ids = []
function sendResults (twitter_ids){
return function(data){
for (var num in data['results']) {
T.get('users/lookup', { screen_name: data['results'][num]['twitter_id'] }, function(err, data, response) {
twitter_ids.push(data[0]['id']);
});
}
console.log(twitter_ids);
}
}
sunlightResults.call(sendResults(twitter_ids));
2 个解决方案
#1
2
Your problem is that you are printing to the console before T.get()
has retrieved any data.
您的问题是您在T.get()检索到任何数据之前打印到控制台。
If you need to wait until multiple callbacks have been called (as per your example), I usually use a helper library function like async.eachSeries(). If you want to do it yourself, something like recursion can be your friend, but might be a little convoluted:
如果你需要等到多个回调被调用(根据你的例子),我通常使用辅助库函数,如async.eachSeries()。如果你想自己做,像递归这样的东西可能是你的朋友,但可能有点复杂:
function lookup(list, index, output, finished) {
if(index >= list.length) { return finished(output); }
var num = list[index];
T.get('users/lookup', { screen_name: data['results'][num]['twitter_id'] }, function(err, data, response) {
output.push(data[0]['id']);
lookup(list, index+1, output, callback);
});
}
var outputList = [];
lookup(data['results'], 0, outputList, function(output){
console.log(output);
});
I am sure some genius here can make this better an more readable, but just a super quick example.
我相信这里的一些天才可以让这个更好,更可读,但只是一个超级快速的例子。
#2
0
Here's an implementation using async
这是使用async的实现
var async = require('async');
var twitter_ids = []
function sendResults (twitter_ids){
return function(data){
async.each(data.results, function (result, done) {
T.get('users/lookup', {
screen_name: result.twitter_id
}, function(err, data, response) {
if (err) {
return done(err);
}
twitter_ids.push(data[0].id);
done();
});
}, function (err) {
if (err) {
throw err;
}
console.log(twitter_ids);
});
}
}
sunlightResults.call(sendResults(twitter_ids));
#1
2
Your problem is that you are printing to the console before T.get()
has retrieved any data.
您的问题是您在T.get()检索到任何数据之前打印到控制台。
If you need to wait until multiple callbacks have been called (as per your example), I usually use a helper library function like async.eachSeries(). If you want to do it yourself, something like recursion can be your friend, but might be a little convoluted:
如果你需要等到多个回调被调用(根据你的例子),我通常使用辅助库函数,如async.eachSeries()。如果你想自己做,像递归这样的东西可能是你的朋友,但可能有点复杂:
function lookup(list, index, output, finished) {
if(index >= list.length) { return finished(output); }
var num = list[index];
T.get('users/lookup', { screen_name: data['results'][num]['twitter_id'] }, function(err, data, response) {
output.push(data[0]['id']);
lookup(list, index+1, output, callback);
});
}
var outputList = [];
lookup(data['results'], 0, outputList, function(output){
console.log(output);
});
I am sure some genius here can make this better an more readable, but just a super quick example.
我相信这里的一些天才可以让这个更好,更可读,但只是一个超级快速的例子。
#2
0
Here's an implementation using async
这是使用async的实现
var async = require('async');
var twitter_ids = []
function sendResults (twitter_ids){
return function(data){
async.each(data.results, function (result, done) {
T.get('users/lookup', {
screen_name: result.twitter_id
}, function(err, data, response) {
if (err) {
return done(err);
}
twitter_ids.push(data[0].id);
done();
});
}, function (err) {
if (err) {
throw err;
}
console.log(twitter_ids);
});
}
}
sunlightResults.call(sendResults(twitter_ids));