javascript - How do I return this object, instead of logging it? -


I have a static site generator for which I listed all the available partial list in the template / partial

  var partials = {'header': 'partials / header', // to ' ./templates/partials/header .html "Footer": 'partial / footer' // './templates/partials/footer.html'};  

I have put this node script together:

  'strict use'; / ** * Dependency * / var fs = Required ("FS"); Var Path = Requirement ("Path"); Var p = 'templates / partial'; / ** * Object with scanned folder and object partial * / fs.readdir (p, function, err, files) {var obj = {}; if (err) {err err;}; for (var i = 0; I & lt; files.length; i ++) {obj [path.basename (files [i], '.html')] = 'partial /' + files [i];}; console.log (obj); });  

When it is executed with the root of the project, it is logged:

  {footer: 'partial / footer html', header : 'Partial / header html'}  

which is good, but I want to return it, do not log in. I have also come to know that what I have written is unlucky, so my question is, how do I return the item, and how do I strengthen it more?

You should provide a callback function:

  function readfile ( Callback) {fs.readdir (p, function (error, files) {var obj = {}; if (mistake) {throw throw;}; (var i = 0; i  

It will be used as follows:

  readfiles (functions (files) {// doo dots with file here}});  

The callback function allows IO to do asynchronously; It often provides a big speed for the code because IO does not need to wait to finish it. In this case, this is not very useful, but the callback pattern is very common in Node.js and hence its use here Have good practice to do


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -