Skip to content

Instantly share code, notes, and snippets.

@arielivandiaz
Last active January 11, 2019 17:15
Show Gist options
  • Save arielivandiaz/c54c80c80f941592d1f4da00b2e92450 to your computer and use it in GitHub Desktop.
Save arielivandiaz/c54c80c80f941592d1f4da00b2e92450 to your computer and use it in GitHub Desktop.
NodeJS Promise
const fs = require('fs');
let createFile = (filename) => {
return new Promise((resolve, reject) => {
if (fs.existsSync(file)) {
reject("File Exists");
} else {
fs.appendFile(file, 'Hello World', (error) => {
if (error) {
reject(error);
} else {
resolve("File Created");
}
});
}
});
}
var file = 'hello.txt';
createFile(file).then((resolve) => {
console.log(resolve);
}).catch((reject) => {
console.log("File Creation Failed");
console.log(reject);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment