Skip to content

Commit

Permalink
Fix returning undefind
Browse files Browse the repository at this point in the history
Fixed returning undefined when using gamedata method for the first time when using cache
  • Loading branch information
King-BR committed Jun 3, 2020
1 parent 38fbc82 commit 9a5632d
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,31 @@ async function requestCache(self, url, type) {
}

if(self.useCache) {
fs.exists('Cache/mozambiqueCache.json', function(existFile) {
fs.exists('./Cache/mozambiqueCache.json', function(existFile) {
if(!existFile) {
fs.exists('Cache', async function(existDir) {
if(!existDir) {
fs.mkdir('Cache', function(err) {})
}
fs.writeFile('Cache/mozambiqueCache.json', '{}', { encoding: 'utf8' }, function(err) {})
fs.writeFile('./Cache/mozambiqueCache.json', '{}', { encoding: 'utf8' }, function(err) {})
})
}
})

await sleep(500)
fs.readFile('Cache/mozambiqueCache.json', async function (err, data) {
fs.readFile('./Cache/mozambiqueCache.json', async function (err, data) {
if (err) return Promise.reject(err);

data = JSON.parse(data);
if (typeof data[`${type}Generated`] !== 'number' || Date.now() >= (data[`${type}Generated`] + self.cacheLifetime)) {
data[type] = await request(self, url);
data[`${type}Generated`] = Date.now();
let json = JSON.stringify(data, null, 2);
fs.writeFile('Cache/mozambiqueCache.json', json, { encoding: 'utf8' }, function (err){});
fs.writeFile('./Cache/mozambiqueCache.json', json, { encoding: 'utf8' }, function (err){});
}
});

await sleep(1100)
const json = require('./Cache/mozambiqueCache.json');
var name = require.resolve('./Cache/mozambiqueCache.json');
delete require.cache[name];
Expand Down

0 comments on commit 9a5632d

Please sign in to comment.