Skip to content

Commit

Permalink
Add Promise support
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Sep 27, 2017
1 parent 4cae403 commit e1be664
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
const fs = require('fs')
const path = require('path')

function fileExists (filepath, options, done = function () {}) {
function fileExists (filepath, options, done) {
if (typeof options === 'function') {
done = options
options = {}
}

if (!done) {
return new Promise(resolve => {
fs.stat(fullPath(filepath, options), (err, stats) => {
if (err) {
return resolve(err.code === 'ENOENT' ? false : err)
}
resolve(stats.isFile())
})
})
}

fs.stat(fullPath(filepath, options), (err, stats) => {
if (err) {
return err.code === 'ENOENT'
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ test('async', t => {
t.notOk(exists, 'non-existing file doesn\'t exist')
done()
})
},
done => {
fileExists('promise-not.here').then(exists => {
t.notOk(exists, 'promise: non-existing file doesn\'t exist')
done()
})
},
done => {
fileExists('.tmp/index.html').then(exists => {
t.ok(exists, 'promise: existing file exists')
done()
})
}
], err => {
rmdir('.tmp', () => t.end())
Expand Down

0 comments on commit e1be664

Please sign in to comment.