A
A
Andrey2020-07-13 18:39:39
Node.js
Andrey, 2020-07-13 18:39:39

Asynchronous execution of JS + node code by example?

Good afternoon!
There is some parser code, the news page is taken as an example.
I want to understand how the code is optimally and correctly written in relation to the execution order? Particularly interested in its asynchronous execution. Perhaps there is a simpler solution.

const request = require ('request');
const cheerio = require ('cheerio');
let news = [];
const ps = new Promise((resolve) => {
request ('https://echo.msk.ru/news/',(error, response, body)=>{
    if (!error && response.statusCode == 200){}
        const $ = cheerio.load (body);
        $('h3 a').each(function() {
           news.push($(this).text());
        });
        resolve(news);
        });
});
ps.then(() => {
    console.log(news);
  });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question