Created
May 10, 2017 01:34
-
-
Save styfle/80454c67c2d6fbed83769c529b615350 to your computer and use it in GitHub Desktop.
Promises in Node.js 8.x Core
This file contains hidden or 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'); | |
function exists(f, callback) { | |
fs.stat(f, (err) => { | |
callback(err ? false : true); | |
}); | |
} | |
function main() { | |
const filename = './example.txt'; | |
const timestamp = new Date().toISOString(); | |
exists(filename, (fileExists) => { | |
if (fileExists) { | |
fs.appendFile(filename, `Updated file on ${timestamp}\n`, (err) => { | |
if (err) { | |
console.err('Failed to update file'); | |
} else { | |
console.log('Successfully updated file'); | |
} | |
}); | |
} else { | |
fs.writeFile(filename, `Created file on ${timestamp}\n`, (err) => { | |
if (err) { | |
console.err('Failed to create file'); | |
} else { | |
console.log('Successfully created file'); | |
} | |
}); | |
} | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment