I am curious about how we can call res.render from within a callback function.
function retrieveUsers(callback){
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) return callback(err);
const users = JSON.parse(data);
return callback(null, users);
});
}
app.get('/', (req,res) => {
retrieveUsers((err,users) =>{
if(err){
res.render('error', {error:err})
} else{
res.render('index', {title: "users", users: users.users});
}
})
});
I'm puzzled by how the callback function is able to utilize the res.render method when it's outside of the actual route and the Callstack is filled with the getUsers(cb) function. How can I determine which values are accessible to callbacks in future scenarios?