Skip to content

Commit 15f3f0b

Browse files
committed
fs: promisify exists correctly
1 parent bb91879 commit 15f3f0b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/fs.js

Lines changed: 10 additions & 1 deletion
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;
@@ -376,6 +376,15 @@ fs.exists = function(path, callback) {
376376
}
377377
};
378378

379+
Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
380+
value: (path) => {
381+
const promise = createPromise();
382+
fs.exists(path, (exists) => promiseResolve(promise, exists));
383+
return promise;
384+
}
385+
});
386+
387+
379388
fs.existsSync = function(path) {
380389
try {
381390
handleError((path = getPathFromURL(path)));

test/parallel/test-fs-promisified.js

Lines changed: 7 additions & 0 deletions
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)