Skip to content

Instantly share code, notes, and snippets.

@mohankumar-i2i
Created February 14, 2020 09:38
Show Gist options
  • Save mohankumar-i2i/4dbab348e9e63fe9e88a97ee813cf595 to your computer and use it in GitHub Desktop.
Save mohankumar-i2i/4dbab348e9e63fe9e88a97ee813cf595 to your computer and use it in GitHub Desktop.
hybrid function
var fs = require('fs');
const cache = {};
function readFile(fileName, callback) {
if (cache[fileName]) {
return callback(null, cache[fileName])
}
fs.readFile(fileName, (err, fileContent) => {
if (err) {
console.log(err);
return callback(err);
}
cache[fileName] = fileContent;
callback(null, fileContent);
});
}
function letsRead(){
readFile('multi.js', (err, result) => {
// error handler redacted
console.log('file read complete');
});
console.log('file read initiated')
}
letsRead()
setTimeout(() => {
letsRead()
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment