Created
September 14, 2018 08:38
-
-
Save devesh2605/3db342f0a3dd46a349c07cfeb20c8487 to your computer and use it in GitHub Desktop.
Find and delete a file in NodeJS using Promise
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'); | |
const deleteFileIfExists = (path) => { | |
return new Promise((resolve, reject) => { | |
try { | |
fs.stat(path, (error, file) => { | |
if (!error && file.isFile()) { | |
fs.unlinkSync(path); | |
resolve(true); | |
} | |
if (error && error.code === 'ENOENT') { | |
console.log('Error ',error); | |
resolve(false); | |
} | |
}); | |
} catch (err) { | |
console.log('Error ',err); | |
reject(err); | |
} | |
}); | |
}; | |
module.exports = {deleteFileIfExists}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment