Call to modules in specific order in node

2019-09-19 12:06发布

问题:

I've used the following code to call two modules, but the invoke action is called before the validate file (I saw in debug). What I should do to verify that validateFile is called before appHandler.invokeAction? Should I use a promise?

var validator = require('../uti/valid').validateFile();
var appHandler = require('../contr/Handler');
appHandler.invokeAction(req, res);

Update

this is the validate file code

var called = false;
var glob = require('glob'),
    fs = require('fs');
module.exports = {

    validateFile: function () {

        glob("myfolder/*.json", function (err, files) {
            var stack = [];
            files.forEach(function (file) {
                fs.readFile(file, 'utf8', function (err, data) { // Read each file
                    if (err) {
                        console.log("cannot read the file", err);
                    }
                    var obj = JSON.parse(data);
                    obj.action.forEach(function (crud) {
                        for (var k in crud) {
                            if (_inArray(crud[k].path, stack)) {

                                console.log("duplicate founded!" + crud[k].path);

                                break;
                            }
                            stack.push(crud[k].path);
                        }
                    })
                });
            });
        });
    }
};

回答1:

Because glob and fs.readFile are async functions and appHandler.invokeAction is invoked during i/o from disk.

Promise is a good solution to solve this but an old school callback could do the job.

validator.validateFile().then(function() {
  appHandler.invokeAction(req, res);
});

and for validate

var Promise = require("bluebird"), // not required if you are using iojs or running node with `--harmony`
    glob = require('mz/glob'),
    fs = require('mz/fs');

module.exports = {
  validateFile: function () {
    return glob("myfolder/*.json").then(function(files) {
      return Promise.all(files.map(function(file) {
        // will return an array of promises, if any of them
        // is rejected, validateFile promise will be rejected
        return fs.readFile(file).then(function (content) {
          // throw new Error(''); if content is not valid
        });
      }));
    })
  }
};

If you want working with promise mz could help :)



回答2:

As the fs.fileRead is async, you should put the code that you want to execute after validateFile to its callback.

The origin could be:

var validator = require('../uti/valid').validateFile();
var appHandler = require('../contr/Handler');
// create a new function that when execute, will call appHandler.invokeAction with req and res given to its arguments.
validator.validateFile(appHandler.invokeAction.bind(null, req, res));

The validator part should be:

var called = false;
var glob = require('glob'),
    fs = require('fs');
module.exports = {
    validateFile: function (callback) {

        glob("myfolder/*.json", function (err, files) {
            var stack = [];
            // Use it to decide whether all files processed or not.
            var filesToLoad = files.length;
            files.forEach(function (file) {
                fs.readFile(file, 'utf8', function (err, data) { // Read each file
                    --filesToLoad;
                    if (err) {
                        console.log("cannot read the file", err);
                        // If the invoke action doesn't depend on the result. You may want to call it here too.
                    }
                    var obj = JSON.parse(data);
                    obj.action.forEach(function (crud) {
                        for (var k in crud) {
                            if (_inArray(crud[k].path, stack)) {

                                console.log("duplicate founded!" + crud[k].path);

                                break;
                            }
                            stack.push(crud[k].path);
                        }
                    })

                    // Only called the callback after all files processed.
                    if (filesToLoad === 0) {
                      callback();
                    }

                });
            });
        });
    }
};

Edit: Thanks for Bergi's remind that there's the files are an array and you have to call the callback when all files is processed, so we have to further use a variable to decide how many files are not processed yet.