Async/Await with Request-Promise returns Undefined

前端 未结 2 1160
无人共我
无人共我 2021-01-04 09:09

I have two files; server.js and scrape.js, below are the code snippets as they currently stand.

server.js:

const scrape = require(\"./scrape\");

asy         


        
相关标签:
2条回答
  • 2021-01-04 09:32

    I believe your go function isn't returning any value.

    You're calling request(options).then(...), but what follows from that promise is never returned by go. I recommend you add a return statement:

    go = async () => {
    
      const options = {
        uri: "http://www.somewebsite.com/something",
        transform: function(body) {
          return cheerio.load(body);
        }
      };
    
      // The only difference is that it says "return" here:
      return request(options)
        .then($ => {
          let scrapeTitleArray = [];
          $(".some-class-in-html").each(function(i, obj) {
            const data = $(this)
              .text()
              .trim();
            scrapeTitleArray.push(data);
          });
          return scrapeTitleArray;
        })
        .catch(err => {
          console.log(err);
        });
    };
    
    0 讨论(0)
  • 2021-01-04 09:47

    You need to return something from your async function (a return inside a then does not return from the main function). Either a promise or something you await-ed.

    Also, make sure to declare your go variable to avoid leaking it into the global space.

    const go = async () => {
    
      const options = {
        uri: "http://www.somewebsite.com/something",
        transform: function(body) {
          return cheerio.load(body);
        }
      };
    
      return request(options)
        .then($ => {
          let scrapeTitleArray = [];
          $(".some-class-in-html").each(function(i, obj) {
            const data = $(this)
              .text()
              .trim();
            scrapeTitleArray.push(data);
          });
          return scrapeTitleArray;
        })
        .catch(err => {
          console.log(err);
        });
    };
    

    Since you are using an async function, you might want to take advantage of the await syntax also.

    const go = async () => {
    
      const options = {
        uri: "http://www.somewebsite.com/something",
        transform: function(body) {
          return cheerio.load(body);
        }
      };
    
      try {
        const $ = await request(options);
        $(".some-class-in-html").each(function(i, obj) {
          const data = $(this)
            .text()
            .trim();
          scrapeTitleArray.push(data);
        });
        return scrapeTitleArray;
      }
      catch (err) {
        console.log(err);
      }
    };
    
    0 讨论(0)
提交回复
热议问题