You are reading a single comment by @allObjects and its replies.
Click here to read the full conversation.
-
...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);
The issue here is this:
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 callwriteFile()
I think you will find it will run fine.