What will be the flow of execution in this node.js code?

94 views Asked by At

Can anyone explain me flow of execution of this code and how...

//router file

/* GET home page. */
Class.getClasses(function(err,classes){
res.render('classes/index', { classes: classes });
},3);

//model

//fetch all classes
module.exports.getClasses = function(callback,limit){
Class.find(callback).limit(limit);
}
1

There are 1 answers

2
Sergio Tulentsev On

You're not familiar with the concept of callbacks, I take it? The logic here is simple. You have your handler

function(err,classes){
  res.render('classes/index', { classes: classes });
}

The rest of your code says "go find me some classes. When you do, call this function". And so you call Class.getClasses which calls Class.find which will invoke your handler when it's done.