'Node.js writes to a file and then file is empty

My Node.js program is working through a long list of tasks, so I am saving progress as a number of processed tasks to a .JSON file to keep track. In about 90% of cases everything is fine but rarely the program produces an empty .JSON file. I have tried checking for null and for an empty String, but it didn't work. The error is still not thrown on write but at the next read. I am confused about it writing an empty file, because the way I understand it even if an invalid value is received for processed_tasks_num then only one value in the JSON should be wrong and the JSON structure itself still intact.

Question: How can I prevent my update_settings function from producing an empty file? Both throwing or catching the error would be fine.

Node v14.17.3; OS Windows 10 Business 21H2

export async function update_settings(logs_category, logs_event, processed_tasks_num) {
    let path "C:\\Users\\MyUser\\MyLogsFolder\\ProgramSettings" + "-" + logs_category + ".json";
    // Checks for invalid values of processed_tasks_num
    if (typeof processed_tasks_num === "undefined" || processed_tasks_num.length === 0) {
       throw new RangeError("processed_tasks_num < start_tasks_num at " + logs_category);
    }

    // readFile from JSON file at path
    async function read_settings(callback) {
        // any async callback invokes callback with response
        fs.readFile(path,async (err, data) => {
            if (err) return callback(err);
            return callback(null, data);
        });
    }

    // JSON.parse(data), update one value, writeFile to same path
    return new Promise((resolve => {
        read_settings(function edit_settings(err, data) {
            // process the async result
            let settings_obj = JSON.parse(data);
            settings_obj[logs_category][logs_event].start_tasks_num = processed_tasks_num;
            let settings_json = JSON.stringify(settings_obj, null, 2);
            if (settings_json === null || settings_json === "") {
                return new Error("empty settings_json at update_settings(), setting_json: "+settings_json);
            }
            fs.writeFile(path, settings_json, (err) => {
                if (err) throw err;
                resolve(0);
            })
        });
    }))
}

The function is always called with await like

await update_settings(logs_category, logs_event, processed_tasks_num);

Any help or pointers in finding a solution are highly appreciated. Thank you.

UPDATE1: I think this error is produced by the function update_settings being called from different points in my program in a non-deterministic manner. So theoretically one function call could intersect with another. So far I thought this would be avoided by the way the Node event loop operates, because all calls of update_settings to one file are made by the same Node process. Now I am questioning this understanding.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source