Last active
January 11, 2019 17:15
-
-
Save arielivandiaz/c54c80c80f941592d1f4da00b2e92450 to your computer and use it in GitHub Desktop.
NodeJS 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'); | |
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