Answer the question
In order to leave comments, you need to log in
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})
Answer the question
In order to leave comments, you need to log in
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? ');
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();
})
}
location /api/ {
...
proxy_pass http://127.0.0.1:8081/;
...
}
location ^~ /api/(.*) {
...
proxy_pass http://127.0.0.1:8081/$1;
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question