• The issue here is this:

    function writeFile() {
      global["writeFile0"](fileNum);
      if (++fileNum < maxFileNum) {
        setTimeout("writeFile()", 100);
      }
    }
    

    This is setting up 100 timeouts that will fire nealry at the same time. I think you are expecting them to queue up.

    If you get rid of the setTimeout and just call writeFile() I think you will find it will run fine.

  • ...sorry, my bad... by tearing it apart I missed the part of calling the right thing from the right place. Here what I wanted to say:

    var fs = require('fs');
    
    var maxFileNum = 100, fileNum = 0;
    
    function writeFile0(i){
       var data = Math.random().toString();
       var name = `file_${i}`;
       var res = fs.writeFile(name, data);
       if (! res) {
         console.log(`did not write!, ${name}, ${data}`);
       }
        setTimeout("writeFile()", 100);
      }
    }
    
    function writeFile() {
      if (++fileNum <= maxFileNum) {
        global["writeFile0"](fileNum);
      }
    }
    
    function onInit() {
      writeFile();
    }
    setTimeout(onInit, 1000);
    
About

Avatar for allObjects @allObjects started