diff --git a/doc/api/fs.markdown b/doc/api/fs.markdown index afae449940..0d62956d83 100644 --- a/doc/api/fs.markdown +++ b/doc/api/fs.markdown @@ -477,7 +477,7 @@ you need to compare `curr.mtime` and `prev.mtime`. Stop watching for changes on `filename`. -## fs.watch(filename, [options], listener) +## fs.watch(filename, [options], [listener]) Stability: 2 - Unstable. Not available on all platforms. diff --git a/lib/fs.js b/lib/fs.js index be8c865a40..f4d9a1ef8f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -773,16 +773,15 @@ fs.watch = function(filename) { listener = arguments[1]; } - if (!listener) { - throw new Error('watch requires a listener function'); - } - if (options.persistent === undefined) options.persistent = true; watcher = new FSWatcher(); watcher.start(filename, options.persistent); - watcher.addListener('change', listener); + if (listener) { + watcher.addListener('change', listener); + } + return watcher; }; diff --git a/test/simple/test-fs-watch.js b/test/simple/test-fs-watch.js index cfae57353f..fc0b00e926 100644 --- a/test/simple/test-fs-watch.js +++ b/test/simple/test-fs-watch.js @@ -58,18 +58,10 @@ try { fs.rmdirSync(testsubdir); } catch (e) { } fs.writeFileSync(filepathOne, 'hello'); -assert.throws( - function() { - fs.watch(filepathOne); - }, - function(e) { - return e.message === 'watch requires a listener function'; - } -); - assert.doesNotThrow( function() { - var watcher = fs.watch(filepathOne, function(event, filename) { + var watcher = fs.watch(filepathOne) + watcher.on('change', function(event, filename) { assert.equal('change', event); if (expectFilePath) { assert.equal('watch.txt', filename); @@ -91,15 +83,6 @@ process.chdir(testDir); fs.writeFileSync(filepathTwoAbs, 'howdy'); -assert.throws( - function() { - fs.watch(filepathTwo); - }, - function(e) { - return e.message === 'watch requires a listener function'; - } -); - assert.doesNotThrow( function() { var watcher = fs.watch(filepathTwo, function(event, filename) {