0

I'm doing crawling with Node.js and I wanna use the return value (which is titleList) out of the function, but that doesn't work out of that function.

Please give me some advice about that.

const axios = require("axios");
const cheerio = require('cheerio');
async function getHTML() 
{
    try 
    {
        return await axios.get("https://google.com");
    } catch (error) {
        console.error(error);
    }
}
getHTML().then(html => 
{
    let titleList = [];
    const $ = cheerio.load(html.data);
    // bodyList에 저장
    const bodyList = $("ul.new_quickMenu_list");

    bodyList.find("li").each(function(i, elem) 
    {
        titleList[i] = {
        title : $(this).find("span").text()
        };
    });
    //console.log(titleList);
    return titleList;
})
Ulegi
  • 1
  • 2
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Hund Aug 19 '19 at 11:16
  • Honestly i don't see the point of using a wrapper function `getHtml` when `axios.get()` already returns you a promise. You can just use that inside the method chain – georoot Aug 19 '19 at 11:27

0 Answers0