Created
February 14, 2020 09:38
-
-
Save mohankumar-i2i/4dbab348e9e63fe9e88a97ee813cf595 to your computer and use it in GitHub Desktop.
hybrid function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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