Skip to content

Instantly share code, notes, and snippets.

@cmmartin
Created May 25, 2017 19:44
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/e1100cd4d931e83524035ba0cbc411d5 to your computer and use it in GitHub Desktop.
Save cmmartin/e1100cd4d931e83524035ba0cbc411d5 to your computer and use it in GitHub Desktop.
Write a file in Node.js, creating its path if necessary. Returns a promise.
const fs = require('fs')
const path = require('path')
const mkpath = require('mkpath')
module.exports = function writeFilePromise(fileName, contents) {
return new Promise((resolve, reject) => {
mkpath(path.dirname(fileName), err => {
if (err) reject(err)
else {
fs.writeFile(fileName, contents, err => {
if (err) reject(err)
else resolve(contents)
})
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment