Skip to content

Commit 6e0eccd

Browse files
dfabulichjasnell
authored andcommitted
fs: promisify exists correctly
PR-URL: #13316 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Vladimir Kurchatkin <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 16605cc commit 6e0eccd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/fs.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const constants = process.binding('constants').fs;
2828
const { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } = constants;
2929
const util = require('util');
3030
const pathModule = require('path');
31-
const { isUint8Array } = process.binding('util');
31+
const { isUint8Array, createPromise, promiseResolve } = process.binding('util');
3232

3333
const binding = process.binding('fs');
3434
const fs = exports;
@@ -308,6 +308,15 @@ fs.exists = function(path, callback) {
308308
}
309309
};
310310

311+
Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
312+
value: (path) => {
313+
const promise = createPromise();
314+
fs.exists(path, (exists) => promiseResolve(promise, exists));
315+
return promise;
316+
}
317+
});
318+
319+
311320
fs.existsSync = function(path) {
312321
try {
313322
handleError((path = getPathFromURL(path)));

test/parallel/test-fs-promisified.js

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ common.crashOnUnhandledRejection();
99

1010
const read = promisify(fs.read);
1111
const write = promisify(fs.write);
12+
const exists = promisify(fs.exists);
1213

1314
{
1415
const fd = fs.openSync(__filename, 'r');
@@ -29,3 +30,9 @@ common.refreshTmpDir();
2930
fs.closeSync(fd);
3031
}));
3132
}
33+
34+
{
35+
exists(__filename).then(common.mustCall((x) => {
36+
assert.strictEqual(x, true);
37+
}));
38+
}

0 commit comments

Comments
 (0)