I have a question about callback functions. Consider this simple code:
var array = [1,2,3];
console.log(array.map(function(val){
return val * 2
}))
This will correctly log back to the console [2,4,6] which means it is "returning" that value back.
Now consider this (NOTE: assume "ab" to be a valid directory)
fs.readdir("ab",function(err,data){
console.log(data)
})
This code works fine and will print an array of filenames in the directory "ab" to the terminal. However, the same code:
console.log(fs.readdir("ab",function(err,data){
return data
}))
This will print undefined.. why is this? if in the previous snippet I can log data to the terminal, why can't I return the value and log it to the console? And how is the first snippet including the map method working using this same logic?
Thank you.