1

I need to return array from then(), but idk how to do this.

If i try to return, it shows me that the function type void. I tried return for the body await, but array was empty

const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const chromedriver = require('chromedriver');
let opt = new chrome.Options();
var result;
async function parser(shotLink, performerLogin) {
    let shots = [];
    let check = new Boolean;
    chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
    opt.addArguments("chrome.switches", "--disable-extentions");
    opt.addArguments("user-data-dir=/home/mike/.config/google-chrome/");
    let driver = await new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).setChromeOptions(opt).build();
  await driver.get('link');
  await driver.findElement(webdriver.By.name('login')).sendKeys('log');
  await driver.findElement(webdriver.By.name('password')).sendKeys('pas', webdriver.Key.ENTER);
  await driver.get('lll' +performerLogin+ '/lll');
  await driver.findElements(webdriver.By.className('className')).then(function(elems){
    let count = 0;
    elems.forEach(function(elem){
      elem.getAttribute('href').then(function(value) {
        count++;
        shots.push(value)
      }).then(() => {
        if(count === elems.length) {
          return shots;
         } 
      })
    })
  })
}

this func return undefined

Thord
  • 21
  • 3
  • You won't be able to do this with `forEach` – charlietfl Sep 01 '19 at 12:58
  • and what can i do, charlietfl? – Thord Sep 01 '19 at 13:01
  • Instead of the `elems.forEach`, do this: `return Promise.all(elems.map(elem => elem.getAttribute("href")));` `map` builds an array of promises. `Promise.all` waits for them all to fulfill (or any of them to reject), fulfilling its promise with the array of results or rejecting it with the rejection reason of the underlying rejected promise. (This is covered by [my answer](https://stackoverflow.com/a/43766002/157247) to the linked question, but the question has so many answers, it's hard to find the relevant information.) – T.J. Crowder Sep 01 '19 at 13:03
  • Can make the `findElements` then callback async and await each `getAttribute` in for loop then return `shots` after loop. And finally add return to `return await driver.findElements` – charlietfl Sep 01 '19 at 13:03

0 Answers0