codemon
codemon

Reputation: 1594

Cannot get array of values from recursive javascript function

I am scraping a list of users who liked a post on instagram. The following code runs perfectly fine:

await click(post);
await sleep(500);
var users = await findAll(user);
var userNames = users.map(async user => {
  var userNameText = await user.getText();
  return userNameText;
});
var result = await Promise.all(userNames);
console.log(result); // ['user1', 'user2'...]

But the modal with the likes only shows 10 users initially. To see the other users, you have to keep scrolling while it only loads a subset at a time. The following recursive function keeps scrolls down loading a single user incrementally for the number of users I want to get:

let likers = [];
await (async function theLoop(i) {
  await driver.sleep(400);
  findAll(user).then(async t => {
    let liker = await t[8].getText();
    await scroll(find(modal)); //Scroll inside the modal
    await likers.push(liker); //PUSH LIKER TO ARRAY
    console.log(liker);
    if (--i) {
      theLoop(i);
    }
  });
})(60);

The problem is that I am unable to get the list of all users from this function. If I do a console.log(likers) if fires immediately before the array is populated. same happens with any function I run after this recursive loop. Nothing I do works.

Upvotes: 0

Views: 63

Answers (1)

Bergi
Bergi

Reputation: 664650

Your problem is that your recursive function doesn't return anything, and you are awaiting neither findAll().then(…) nor the recursive call. Also you don't need recursion at all here, with async/await you can (should) write a plain loop:

for (var i=60; --i; ) {
  await driver.sleep(400);
  const t = await findAll(user); // don't use `then`
  let liker = await t[8].getText();
  await scroll(find(modal));
  likers.push(liker); // nothing to `await` here
  console.log(liker);
}

Upvotes: 2

Related Questions