I am currently going through the nodeschool.io tutorial on Javascript. This is a sample code from one of the solutions.
module.exports = function (dir, filterStr, callback) {
fs.readdir(dir, function (err, list) {
if (err)
return callback(err)
list = list.filter(function (file) {
return path.extname(file) === '.' + filterStr
})
callback(null, list)
})
}
From what I understand, javascript is a synchronous language. However, node.js uses asynchronous functions. In this code, isn't it possible for the callback to happen before the if statement. Therefore, is there even any reason to have functions that are different form the format
function(params, callback){
// one line data manipulation
callback();
}
Aucun commentaire:
Enregistrer un commentaire