简体   繁体   中英

NodeJs : Code sometimes work - Async.map issue

I have a config json file as below.

"constraints": {
    "input": "input",
    "output": "output"
  }

I am trying to read this file and create input and output directory with child directories.

var fs = require("fs");
var async = require("async");
var node_xj = require("xls-to-json");
var cf= JSON.parse(fs.readFileSync("config.json", 'utf8'));

// Declare variables
var files = [];    

function readAsync(file, callback) {
    node_xj(file, callback);        
} 

function create(currentDirPath, outputDirPath, callback) {  
// some code creating directories
}    

create(cf.lang.input, cf.lang.output, function(stat, config) {
    files.push(config); 
});  

async.map(files, readAsync, function(err, results) {
     if(err) throw err;
});

The code works fine, but its sometimes does not. Let me walk through the code. I am loading some modules.

var fs = require("fs");
var async = require("async");
var node_xj = require("xls-to-json");

I am loading the config file.

var cf= JSON.parse(fs.readFileSync("config.json", 'utf8'));

Then i am passing the cf file to create function which after its operation returns me an an object which i push it into an array .

var files = [];    
function readAsync(file, callback) {
    node_xj(file, callback);        
} 

function create(input, output, callback) {  
   // some code creating directories and object
}    

create(cf.lang.input, cf.lang.output, function(stat, config) {
    files.push(config); 
});  

async.map(files, readAsync, function(err, results) {
     if(err) throw err;
});

Then i am passing the files array to my async.map function which passes it to readAsync function for another operation.

Question:

  1. Can anyone tell me whether the way i have written the code flow makes if flaw sometimes.
  2. Is there a better way of writing the same code logic flow.
  3. Should i use async.map to iterate files and then pass it to readAsync

If you're depending on asynchronous operations to occur before other operations, you have to treat them asynchronously! Your code as described seems to create some directories (which will take measurable time) and immediately attempts to use them, which isn't kosher.

You might consider something like:

async.series([
  function(){
    // create your directories here
  },
  function(){
    async.map(files,...)
  }
]);

which will guarantee that the resources needed by your map exist before the map is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM