I've been learning Node for only few days now. The reason why I decided to switch to Node is that it's way faster than php, which I used to use as a server side language. Recently, I realized that using php is not that effective when it comes to sending requests and processing data in a recursive manner my previous post considering php and VK api. (By the way, I found a solution to that one)
So I rewrote my function to Node and now it doesn't work properly due to the async nature of Node.
To put it short: this function must find all the indirect connections between users of a social network and fill an array connections with data about each found indirect connection (which is also an array that contains strings of data about each chain member (i.e. name, second_name and id)).
var find_links = function(n, node1, ports,
vk, visited, connection, connections, event_count, callback) {
n+=1;
if(n<2) { // I don't want to go too far with my recursion. I keep track of it
// vk.request just send a request to api and gets a list of friends of a user with user_id: node1
vk.request('friends.get', {'user_id' : node1, 'fields' : 'domain, first_name'}, function(_o) {
// when we get response it may be either response or 'error' if user has deleted their account
if('response' in _o) { // just checking if we have response
for (var item of _o['response']['items']) {
for (var port of ports['response']['items']) {
if(port['id'] == item['id']) {
data = item['first_name'] + ' ' + item['last_name'] + ' ' + item['id']; // info about user
connection.push(data);
connections.push(connection);
connection.pop();
break;
}
else {
if (!visited.present(item['id'])) { // if we haven't encountered this user before
data = item['first_name'] + ' ' + item['last_name'] + ' ' + item['id']; // info about user
connection.push(data);
visited.push(item['id']); // we add user_id to visited array
// recursion
find_links(n, item['id'], ports, vk, visited, connection, connections, event_count, console.log);
connection.pop();
}
}
}
}
if (n == 1) {
callback("conn is "+connections);
}
}
else {
return 1;
}
});
}
else { // if n == 2, i check only friends of current user and return
vk.request('friends.get', {'user_id' : node1, 'fields' : 'domain, first_name'}, function(_o) {
if('response' in _o) {
for(item_ of _o['response']['items']) {
for(var arg_ of ports['response']['items']) {
if (arg_['id'] === item_['id'] && !visited.present(item_['id'])) {
data = item_['first_name'] + ' ' + item_['last_name'] + ' ' + item_['id'];
connection.push(data); // add data about user to connection
connections.push(connection);
connection.pop();
break;
}
else {
connection.pop();
}
}
}
}
return 1;
})
}
}
So, when I log connections array which must contain data about all the chains found, I get this ,,,,,,,,,,,,,,,,. Items just aren't pushed to the array, are they?
So, how can I get my code run in a loop sequentially and wait for the recursive function to finish before proceeding?
Response from VK when I request friends looks like this:
{ response:
{ count: 67,
items:
[ { id: 6248811,
first_name: 'Екатерина',
last_name: 'Федорова',
domain: 'k.fedorova' },
{ id: 8102967,
first_name: 'Роман',
last_name: 'Соколинский',
domain: 'majestyk',
hidden: 1 },
.....
]
}
}