Async read and write keys

This commit is contained in:
Vladislav Hristov committed 2018-06-27 17:59:35 +03:00
1 parent 9cfed8ec81
commit 9660242234
5 files changed
+52 -32

No files matched your search

+16 -6
View File
@@ -6,14 +6,24 @@ module.exports = (pathToFile) => {
const path = pathToFile;
return {
read() {
if (!fs.existsSync(path))
return null;
return fs.readFileSync(path, { encoding: 'utf8' });
write(data) {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, { encoding: 'utf8' }, (error) => {
if (error) throw error;
return resolve();
});
});
},
write(data) {
fs.writeFileSync(path, data, { encoding: 'utf8' });
read() {
return new Promise((resolve, reject) => {
fs.readFile(path, { encoding: 'utf8' }, (error, data) => {
if (error) reject(error);
return resolve(data);
});
});
},
};
};