return values of asynchronous methods javascript

0

Hello, I have a problem here, I need to use the node request module twice. one to get the link from one page and the other to get the contents of a post.

This way, I have the following problem:

request('https://www.pensarcontemporaneo.com/, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    const $ = cheerio.load(body);

    // na página inicial ele vai pegar o link de uma postagem recente...
    const href = $('.td_block_inner').find('.td-module-thumb').children().first().attr('href');

    // aqui ele vai entrar neste link pra pegar o conteúdo da postagem
    request(href, function (error2, response2, body2) {
      const $2 = cheerio.load(body);
      // preste atenção, eu preciso que essa variavel vá para dentro da array article
      var content = $2('.td-ss-main-content').html();
      console.log(content); // aparece o conteúdo
    });

    var article = {
      // só que aqui ele chega vazio
      content: content,
      link: href,
      thumb: $('.td_block_inner').find('img').first().attr('src'),
      title: $('.td_block_inner').find('.entry-title').children().first().text(),
    }
  }
});

I come from php so I do not have much experience with javascript, much less with node, so very disoriented.

    
asked by anonymous 10.03.2018 / 01:31

0 answers