1.0.2 • Published 4 years ago
files-json v1.0.2
filesJSON
Open a json file, read the content into this and write the this's content back to the json file. Version that uses Promises!
const { filesJSON, FileJSON } = require("files-json");
const test1 = async () => {
const monkey1 = await new FileJSON("monkey.json");
monkey1.says = "hoehoehaha";
console.log("test1, monkey1:", monkey1);
// "test1, monkey1: FileJSON { says: 'hoehoehaha' }"
await monkey1.write();
// monkey.json did not exist yet and will be created
const monkey2 = await new FileJSON("monkey.json");
console.log("monkey1 === monkey2:", monkey1 === monkey2);
monkey1.close();
console.log(filesJSON);
monkey2.close();
console.log(filesJSON, "<-- empty, BUT WAIT monkey2-->", monkey2);
// class FileJSON has removed all references to FileJSON("monkey.json")
// However, monkey1 and monkey2 within this scope
// still reference to FileJSON("monkey.json")
};
const test2 = () => {
new FileJSON("monkey.json").then(monkey3 => { // reads data from file
console.log("test2, monkey3:", monkey3);
// "test2, monkey3: FileJSON { says: 'hoehoehaha' }"
monkey3.close();
});
};
(async function test() {
await test1();
// all references to the FileJSON("monkey.json") are now gone, it is garbage collected
test2();
})();