Skip to content

Commit

Permalink
fs: use createDeferredPromise() in promises.watch()
Browse files Browse the repository at this point in the history
This commit updates fsPromises.watch() to use the
createDeferredPromise() utility.

PR-URL: #37386
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
cjihrig authored and targos committed Feb 28, 2021
1 parent 4b54c10 commit d141fce
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions lib/internal/fs/watchers.js
Expand Up @@ -4,7 +4,6 @@ const {
FunctionPrototypeCall,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Promise,
Symbol,
} = primordials;

Expand All @@ -15,6 +14,7 @@ const {
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
const { createDeferredPromise } = require('internal/util');

const {
kFsStatsFieldsNumber,
Expand Down Expand Up @@ -319,21 +319,14 @@ async function* watch(filename, options = {}) {
throw new AbortError();

const handle = new FSEvent();
let res;
let rej;
let { promise, resolve, reject } = createDeferredPromise();
const oncancel = () => {
handle.close();
rej(new AbortError());
reject(new AbortError());
};

try {
signal?.addEventListener('abort', oncancel, { once: true });

let promise = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
});

handle.onchange = (status, eventType, filename) => {
if (status < 0) {
const error = uvException({
Expand All @@ -343,11 +336,11 @@ async function* watch(filename, options = {}) {
});
error.filename = filename;
handle.close();
rej(error);
reject(error);
return;
}

res({ eventType, filename });
resolve({ eventType, filename });
};

const err = handle.start(path, persistent, recursive, encoding);
Expand All @@ -366,10 +359,7 @@ async function* watch(filename, options = {}) {

while (!signal?.aborted) {
yield await promise;
promise = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
});
({ promise, resolve, reject } = createDeferredPromise());
}
throw new AbortError();
} finally {
Expand Down

0 comments on commit d141fce

Please sign in to comment.