diff --git a/index.js b/index.js index a2a12f61..676d715b 100644 --- a/index.js +++ b/index.js @@ -480,7 +480,7 @@ unwatch(paths_) { /** * Close watchers and remove all listeners from watched paths. - * @returns {FSWatcher} for chaining. + * @returns {Promise}. */ close() { if (this.closed) return this; @@ -488,7 +488,11 @@ close() { // Memory management. this.removeAllListeners(); - this._closers.forEach(closerList => closerList.forEach(closer => closer())); + const closers = []; + this._closers.forEach(closerList => closerList.forEach(closer => { + const promise = closer(); + if (promise instanceof Promise) closers.push(promise); + })); this._streams.forEach(stream => stream.destroy()); this._userIgnored = undefined; this._readyCount = 0; @@ -497,7 +501,7 @@ close() { ['closers', 'watched', 'streams', 'symlinkPaths', 'throttled'].forEach(key => { this[`_${key}`].clear(); }); - return this; + return closers.length ? Promise.all(closers).then(() => undefined) : Promise.resolve(); } /** diff --git a/test.js b/test.js index e78c9f8d..9a61a915 100644 --- a/test.js +++ b/test.js @@ -2078,10 +2078,10 @@ describe('chokidar', function() { currentDir = getFixturePath(''); }); - afterEach(() => { + afterEach(async () => { let watcher; while ((watcher = allWatchers.pop())) { - watcher.close(); + await watcher.close(); } }); diff --git a/types/index.d.ts b/types/index.d.ts index 36b0c298..af0b5585 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -38,7 +38,7 @@ export class FSWatcher extends EventEmitter implements fs.FSWatcher { /** * Removes all listeners from watched files. */ - close(): void; + close(): Promise; on(event: 'add'|'addDir'|'change', listener: (path: string, stats?: fs.Stats) => void): this;