Skip to content

Instantly share code, notes, and snippets.

@cmmartin
Last active May 25, 2017 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmmartin/c2a137921759ceb08107bb44e69674f2 to your computer and use it in GitHub Desktop.
Save cmmartin/c2a137921759ceb08107bb44e69674f2 to your computer and use it in GitHub Desktop.
Read a file in Node.js. Returns a promise.
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