Easy question about readText and writeText (related to async reading) - Nativescript Core



Hi, I’m trying to ensure here that my “writeText” process (done inside the getJson .then function) does not conflict with the file being read. I’m not sure, but doing this code steps shown below, that should never happen, right ?
I mean, I read the file first, then, asyncroniusly send the data to my Array. So, even if the writeFile (that will update that same file read before with new info) is outside the .then, it should not intefere with the async reading… as there’s no data being modified during the read process and “r” will already have the info when the writeText arrives, am I right ?

Or is it better to just move the getJson are write inside the .then of the readText just in case ?

This is my simplfied example:

    // Read File and process later via ASYNC function
    try {
        file.readText().then(function (r) {
            try {
                const readData = JSON.parse(r);
                currentDataVer = readData.version;
                for (var key in readData.data) {
                    var registro = readData.data[key];
                agendaVM.set("agendaList", agendaList);
            } catch (err) {
                // Log HERE Parsing Error
    } catch (err) {
        reject(err); // Log error during Read File

    // Check getJSON and save file if it's new
    http.getJSON(myUrl).then(function(r) {
		const readData = r;
		// Compare current version with getjson version
        if (readData.version === currentDataVer) {
			// if equal do nothing
		} else {
	}, function (e) {
		// Log HERE getJSON Error


I believe your JSON is not so large, so it should not interfere. If you want the latest always, trigger read after writing.


Thanks, well actually the idea is to read first the local file and then check for updates via getJson and inform the user if there’s an update. Read first is to show information FAST to the user, then if there’s an update inform them and let them choice to refresh or not.

My main doubt was, actually, if reading the file (which is an async process) could be interupted by the writeText inside the getJson in cases, weird one’s probably, where the json is very small and the getjson could be SO fast that returns before the actual read ends, so it could be getting a file access conflict or something like that and never be able to save the new text.