Files
Node.js-14-Cookbook/Chapter03/fetching-meta-data/checking-file-existence/check.js
T
2017-07-31 11:33:31 +05:30

17 lines
430 B
JavaScript

'use strict'
const fs = require('fs')
const exists = (file) => new Promise((resolve, reject) => {
fs.access(file, (err) => {
if (err) {
if (err.code !== 'ENOENT') { return reject(err) }
return resolve({file, exists: false})
}
resolve({file, exists: true})
})
})
exists(process.argv[2])
.then(({file, exists}) => console.log(`"${file}" does${exists ? '' : ' not'} exist`))
.catch(console.error)