Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a promise if no callback specified #40

Merged
merged 3 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@ function toMatcherFunction(ignoreEntry) {
}

function readdir(path, ignores, callback) {

if (typeof ignores == 'function') {
callback = ignores
ignores = []
}

if (!callback) {
return new Promise(function (resolve, reject) {
readdir(path, ignores || [], function (err, data) {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}

ignores = ignores.map(toMatcherFunction)

var list = []
Expand Down
19 changes: 19 additions & 0 deletions test/recursive-readdir-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,23 @@ describe('readdir', function() {
done()
})
})

if (!global.Promise) {
console.log("Native Promise not supported - skipping tests")
} else {
it('works with promises', function(done) {

var expectedFiles = getAbsolutePaths([
'/testdir/a/a', '/testdir/a/beans',
'/testdir/b/123', '/testdir/b/b/hurp-durp',
'/testdir/c.txt', '/testdir/d.txt'
])

readdir(p.join(__dirname, 'testdir'))
.then(function(list) {
assert.deepEqual(list.sort(), expectedFiles.sort())
done()
})
})
}
})