I basically I have two functions :

  1. One to read a file (post) : loadPost(name)
  2. One to read all files : loadPosts()

Obviously loadPosts() calls loadPost(name).

Both return the final html.

Ideally I should write it asynchronously.

The problem is : I don't know how to make that asynchronously, as I need to wait until the file is completely read before I can continue.

Here is my synchronous solution:

function loadPost(name){
    var post = fs.readFileSync("_posts/"+name,'utf8');
    // Convert Markdown to html
    var marked = mark(post);
    return marked;
}

function loadPosts(){
    var files = fs.readdirSync("_posts/");
    var html;
    for(var i = 0; i < files.length ; i++){
        html += loadPost(files[i]);
    }
    return html;
}
有帮助吗?

解决方案

Something like this, no third party libs necessary. Fairly simple really.

/*jshint node:true */

function loadPosts(callback) {
    'use strict';

    var allHtml = ''; //Declare an html results variable within the closure, so we can collect it all within the functions defined within this function

    fs.readdir("_posts/", function (err, files) {

        function loadPost(name) {
            fs.read("_posts/" + name, 'utf8', function (err, data) {

                allHtml += mark(data);//Append the data from the file to our html results variable

                if (files.length) {
                    loadPost(files.pop()); //If we have more files, launch another async read.
                } else {
                    callback(allHtml); //If we're out of files to read, send us back to program flow, using the html we gathered as a function parameter
                }
            });
        }

        loadPost(files.pop());
    });
}

function doSomethingWithHtml(html) {
    'use strict';
    console.log(html);
}

loadPosts(doSomethingWithHtml);

其他提示

You'll want to use the async.js package to make your life easier.

function loadPost(name, callback) {
  fs.readFile("_posts/+"name,{encoding:'utf8'},function(err, data) {
    if(err) return callback(err);
    // convert markdown to html
    var marked = mark(post);
    callback(false, marked);
  });
}

function loadPosts(cb) {
  fs.readdir("_posts/", function(err, dir) {
    if(err) return cb(err);
    var html;
    async.eachSeries(dir, function(one, callback) {
      loadPost(one, function(err, post) {
        if(err) return cb(err);
        html += post;
        callback(false);
      });
    },
    // final callback
    function(err) {
      if(err) { console.log(err); cb(true); return; }
      cb(false, html);
    });
  });
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top