Created
May 25, 2017 19:44
-
-
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.
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') | |
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