1.0.2 • Published 4 years ago

files-json v1.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

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();
})();
1.0.2

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago