I've recently picked up Node and see that things don't always run sequentially. I'm pretty confused as I'm used to
1) Assignment
2) Print data
Current I'm running the below function and calling var x = searchForProfessor("prof_name_here");
I then call console.log(x);
only to get undefined.
I've been reading about callbacks all over the web and I can't wrap my head around the idea and apply it to this code. Can someone give me some intuition into making the above possible with callbacks?
My Function
var searchForProfessor = function searchForProfessor(teacher_name) {
google.resultsPerPage = 10
var nextCounter = 0
google(teacher_name, function (err, res){
for (var i = 0; i < res.links.length; ++i) {
var link = res.links[i];
if (!link.title.includes('Add') || !link.title.includes('RATINGS') || !link.title.includes("Hint")) {
request(link, function(err, resp, body){
if (!err && resp.statusCode == 200) { //If no error is going to happen, then print the data
var $ = cheerio.load(body); //Grab the body of data from 'prof_link'
var overall_rating = $('.breakdown-header .grade').text(); //Get the grade rating from the following classifications text
if (overall_rating.substr(0,3)) {
teacher_results.push(prof_name);
} //End if
} //End if
}); //End request
}//End if for comparisons ||
} //End For
}); //End google function
} //End searchForProfessor