I
I
input_denis2021-02-02 18:47:08
Node.js
input_denis, 2021-02-02 18:47:08

How to wait for user input before executing asynchronous code?

Hello!
Please help me understand using readline in node.js.

The bottom line is that I'm doing page parsing. For some reason, not all of them manage to find an element by the selector (this is a separate question, but now about something else). In case the element responsible for the number of pages is not found, I want to enter it manually via the command line. I wrote the code, but the script does not wait for the parameter to be entered, but continues to run and naturally falls off without the number of pages.

Here is the code:

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

            const lastPageLink = await this.driver.findElement(By.css('.pager-last a'))
            let count_pages = +lastPageLink.getText()

            if (count_pages !== count_pages) {
                rl.question('Не нашли количество страниц. Установите вручную ', (value) => {
                    count_pages = value
                    rl.close()
                })
            }
            console.log('Total page count: ' + count_pages)
            // Цикл по страницам
            for (let page = this.last_page ? this.last_page : 1; page < count_pages; page++) {
                //update last page
                await this.updateEntity('sites', {last_page: page}, {name: this.SITE}).last_page

                await this.driver.get(`${this.currentCategory.url}?page=${page}`)
                console.log('New page parse: ' + page)
                await this.parsePage()
            }
            // Парсинг завершен, отмечаем в базе
            await this.updateEntity('categories', {parsing: false}, {url: this.currentCategory.url})


The problem is that I can't just shove everything into rl.question. First, he will complain that I am using async code outside of an async function. Secondly, it turns out that I have to duplicate the code: specify it for rl.question and specify it for the situation when the number of pages was obtained in the normal mode.

How can I make the script wait for my input first and only then start to "Loop Through Pages"?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
input_denis, 2021-02-03
@input_denis

In general, if anyone is interested, I solved my problem of synchronous reading from the command line with the readline-sync module

var readlineSync = require('readline-sync');
 
// Wait for user's response.
var userName = readlineSync.question('May I have your name? ');

I read about it even before creating the question, but yesterday for some reason the package gave 404th. Today install without problems.

A
Alexander Cheremkhin, 2021-02-02
@Che603000

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

            const lastPageLink = await this.driver.findElement(By.css('.pager-last a'))
            let count_pages = +lastPageLink.getText()

            if (count_pages !== count_pages) {
                rl.question('Не нашли количество страниц. Установите вручную ', (value) => {
                     console.log(value); 
                     // ---------- begin code ----
                     // поместите сюда весь код который использует введенное кол-во страниц 
                    // ----------- end code ----
                    rl.close();
                })
           }

L
Lynn "Coffee Man", 2017-08-02
@ezpy

location /api/ {
    ...
    proxy_pass http://127.0.0.1:8081/;
    ...
}

https://serverfault.com/a/586614/211028

E
egor_nullptr, 2017-08-02
@egor_nullptr

location ^~ /api/(.*) {
    ...
    proxy_pass http://127.0.0.1:8081/$1;
    ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question