Async read and write keys

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

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);
});
});
},
};
};