From 15f3f0b4833f20ee4af4e87cdec1c02aa7625688 Mon Sep 17 00:00:00 2001 From: Dan Fabulich Date: Tue, 30 May 2017 14:34:27 -0700 Subject: [PATCH] fs: promisify exists correctly --- lib/fs.js | 11 ++++++++++- test/parallel/test-fs-promisified.js | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index 37dcba0b3bd517..9f3a1fad5b00af 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -28,7 +28,7 @@ const constants = process.binding('constants').fs; const { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } = constants; const util = require('util'); const pathModule = require('path'); -const { isUint8Array } = process.binding('util'); +const { isUint8Array, createPromise, promiseResolve } = process.binding('util'); const binding = process.binding('fs'); const fs = exports; @@ -376,6 +376,15 @@ fs.exists = function(path, callback) { } }; +Object.defineProperty(fs.exists, internalUtil.promisify.custom, { + value: (path) => { + const promise = createPromise(); + fs.exists(path, (exists) => promiseResolve(promise, exists)); + return promise; + } +}); + + fs.existsSync = function(path) { try { handleError((path = getPathFromURL(path))); diff --git a/test/parallel/test-fs-promisified.js b/test/parallel/test-fs-promisified.js index 12bd5d6fa1f954..ac6e22f9690821 100644 --- a/test/parallel/test-fs-promisified.js +++ b/test/parallel/test-fs-promisified.js @@ -9,6 +9,7 @@ common.crashOnUnhandledRejection(); const read = promisify(fs.read); const write = promisify(fs.write); +const exists = promisify(fs.exists); { const fd = fs.openSync(__filename, 'r'); @@ -29,3 +30,9 @@ common.refreshTmpDir(); fs.closeSync(fd); })); } + +{ + exists(__filename).then(common.mustCall((x) => { + assert.strictEqual(x, true); + })); +}