0

I am very new to JavaScript, trying to understand the concepts of asynchronous function. So, basically I wrote a webScraper for RateMyProfessor using nightmare. This is the function:

var Nightmare = require("nightmare"),
  nightmare = Nightmare();
  
const baseURL =
  "https://www.ratemyprofessors.com/search.jsp?queryBy=schoolId&schoolName=University+of+California+Santa+Barbara&schoolID=1077&queryoption=TEACHER";

const getRatingProfessor = (professorName) => {
  let rating;
  let nameSplitter = professorName.split(" ");
  if (nameSplitter.length > 2) {
    professorName = nameSplitter[0] + " " + nameSplitter[1];
  }
  nightmare
    .goto(baseURL)
    .click("#ccpa-footer > .close-this")
    .type("#professor-name", professorName)
    .wait(1000)
    .evaluate(() => {
      var resultList = document.querySelectorAll("a .rating");
      //if no result is found
      if (typeof resultList === "undefined" || resultList.length == 0) {
        return "Not Found";
      }
      //Found the professor with exact name
      if (resultList.length == 1) {
        return document.querySelector("a > .rating").innerHTML;
      }
      //conficting similar professor names (rare case)
      if (resultList.length >= 2) {
        return "Cannot Determine";
      }
    })
    .end()
    .catch((err) => {
      console.log(err);
    })
    .then((text) => {
      rating = text;
      console.log(professorName, text);
    });
    return rating;
};
console.log(getRatingProfessor("XXXXX"));

If I run this program, it gives the following output:

undefined
SomeProfName 4.8

It seems like the function returned the rating to console.log without waiting for the promise. Why isn't function not waiting for the nightmare promise to get resolved. More importantly, why isn't the value of rating getting updated; or it has been updated but console.log doesn't want to wait for the function?

Sorry, these questions may look absurd, but I would really appreciated the answers :0

  • try using Async and Await for your Code. Resourse : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function – Sarthak Kuchhal Aug 22 '20 at 22:50

1 Answers1

0

Your function explicitly does not return anything hence will return undefined by default. What you are getting is correct but within the then()...

If you wanted to return an async result you'd need to declare your function as async and then await the result.

const getRatingProfessor = async (professorName) => {
  let rating;
  let nameSplitter = professorName.split(" ");
  if (nameSplitter.length > 2) {
    professorName = nameSplitter[0] + " " + nameSplitter[1];
  }
  await text = nightmare...
  return text;
};


(async () => {
   console.log(await getRatingProfessor("XXXXX"));
  }
)()
EugenSunic
  • 13,162
  • 13
  • 64
  • 86