I have a simple code to write a js list to a js file.
// Requiring fs module in which
// writeFile function is defined.
var fs = require('fs');
// first overwrite any pre-existing file with the start of the list
fs.writeFile('Output.js', 'export const addressPoints = [', function (err) {
if (err) throw err;
console.log('Create file Output.js');
});
for (let i = 0; i < 100; i++) {
// var string_row = "[" + latlon_datum1_row[i] + "],";
var string_row = "HI"
console.log(string_row);
fs.appendFile('Output.js', string_row, function (err) {
if (err) throw err;
console.log('Writing file contents line', i);
});
}
fs.appendFile('Output.js', '];', function (err) {
if (err) throw err;
console.log('Append file Output.js');
});
It can be thought of in three parts, one intial component creates the new js script and adds 'export const addressPoints = [' to it. After that, a for-loop runs to write the items of the list, and lastly, '];' is appended to the end of the file.
Very strangely, the code does not work as intended. One would expect the console log to look something like
Create file Output.js
Writing file contents line 1
Writing file contents line 2
Writing file contents line 3
...
Writing file contents line 99
Append file Output.js
However, the output is scrambled and written in the wrong order (showing last few lines):
...
Writing file contents line 96
Writing file contents line 97
Writing file contents line 98
Append file Output.js
Writing file contents line 99
Writing file contents line 91
Writing file contents line 79
Writing file contents line 68
Is there a method that can do this more quickly? If not, why is this error occurring? I assume it has something to do with fs closure but I am not too familiar with js.