I am wondering how are we able to use res.render inside a callback
function getUsers(cb){
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) return cb(err);
const users = JSON.parse(data);
return cb(null, users);
});
}
app.get('/', (req,res) => {
getUsers((err,users) =>{
if(err){
res.render('error', {error:err})
} else{
res.render('index', {title: "users", users: users.users});
}
})
});
Because when we pass the callback, Callstack gets loaded up with getUsers(cb) Function, and we are no longer inside the route, then how the callback is using the res.render Method? And how do I know moving forward, which values is accessible to callbacks?