Last active
May 25, 2017 20:02
-
-
Save cmmartin/c2a137921759ceb08107bb44e69674f2 to your computer and use it in GitHub Desktop.
Read a file in Node.js. Returns a promise.
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
const fs = require('fs') | |
module.exports = function readFilePromise(path, encoding) { | |
return new Promise((resolve, reject) => { | |
try { | |
var filename = require.resolve(path) | |
fs.readFile(filename, encoding || 'utf8', (err, file) => { | |
if (err) reject(err) | |
else resolve(file) | |
}) | |
} catch (e) { | |
reject(e) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment