D
D
Daniel Chistyakov2021-03-28 17:03:23
Node.js
Daniel Chistyakov, 2021-03-28 17:03:23

How to execute this logic synchronously?

Good day! I am currently writing a backend in Node.JS. I have an array of links arr.
I need to perform the following operations using each element of this array. Having written the code below, I came to the conclusion that it mapis not being executed in the sequence that I need - hence the errors.

The code is currently executing in this order:
Source link before condition url[0]
Source link before condition url[1]
Start of negative condition
Source link in negative condition url[0]
Start of negative condition
Source link in negative condition url[1]
End of negative condition
End of negative condition

Required order:
Source link before conditionurl[0]
Checking the condition for url[0]
Source link before the condition url[1]
Checking the condition for url[1]
Output values, should get:

[
  { vat: vat, url: url },
  { vat: vat, url: url }
]


This code uses puppeteer, everything related to it starts with: " await page".

Actually code:
const promises = arr.map(async (url, key) => {
    console.log('Исходная ссылка до условия ' + url);
    await page.goto(url);
    await page.waitForSelector('td.sp-properties-form__value');

    if (await page.$eval('td.title-card-page__right-column > table.company-stats > tbody > tr.company-stats__item:nth-child(2) td.company-stats-item__label span.text-mutted', el => el.textContent) === "Головная компания") {
      console.log('Начало позитивного условия');
      const newurl = await page.$eval('td.title-card-page__right-column > table.company-stats > tbody > tr.company-stats__item:nth-child(2) span.white-space-normal > a', el => el.href);
      console.log('Новая ссылка ' + newurl)
      await new Promise(r => setTimeout(r, getRandom()));
      await page.goto(newurl);
      await page.waitForSelector('td.sp-properties-form__value');
      const vat = await page.$eval('td.company-reg-codes span.sp-delimited-list__item:nth-child(2) span.entity-reg-code__value span', el => el.textContent);
      res[key] = { vat: vat, url: newurl };
      console.log('Конец позитивного условия');
    } else {
      console.log('Начало негативного условия');
      console.log('Исходная ссылка в негативном условии ' + url)
      const vat = await page.$eval('td.company-reg-codes span.sp-delimited-list__item:nth-child(2) span.entity-reg-code__value span', el => el.textContent);
      res[key] = { vat: vat, url: url };
      console.log('Конец негативного условия');
    }
    return res[key]
  })
  const result = await Promise.all(promises)
  console.log(result)


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
user_of_toster, 2021-03-28
@danielchistyakov

const f = async (url, key) => {...};

async function* gen(arr) {
  for (let i = 0; i < arr.length; i++) {
    await f(arr[i].url, arr[i].key);
    yield i;
  }
}

res = [];

(async () => {
  let iter = gen(arr);
  for await (let value of iter) {
    res.append(value);
  }
})();

How to make a sequence of loop iterations with asynchronous code?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question