fs: promisify exists correctly
PR-URL: https://github.com/nodejs/node/pull/13316 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
This commit is contained in:
parent
38cec08ff7
commit
65531d0e86
11
lib/fs.js
11
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 { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } = constants;
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const pathModule = require('path');
|
const pathModule = require('path');
|
||||||
const { isUint8Array } = process.binding('util');
|
const { isUint8Array, createPromise, promiseResolve } = process.binding('util');
|
||||||
|
|
||||||
const binding = process.binding('fs');
|
const binding = process.binding('fs');
|
||||||
const fs = exports;
|
const fs = exports;
|
||||||
@ -308,6 +308,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) {
|
fs.existsSync = function(path) {
|
||||||
try {
|
try {
|
||||||
handleError((path = getPathFromURL(path)));
|
handleError((path = getPathFromURL(path)));
|
||||||
|
@ -9,6 +9,7 @@ common.crashOnUnhandledRejection();
|
|||||||
|
|
||||||
const read = promisify(fs.read);
|
const read = promisify(fs.read);
|
||||||
const write = promisify(fs.write);
|
const write = promisify(fs.write);
|
||||||
|
const exists = promisify(fs.exists);
|
||||||
|
|
||||||
{
|
{
|
||||||
const fd = fs.openSync(__filename, 'r');
|
const fd = fs.openSync(__filename, 'r');
|
||||||
@ -29,3 +30,9 @@ common.refreshTmpDir();
|
|||||||
fs.closeSync(fd);
|
fs.closeSync(fd);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
exists(__filename).then(common.mustCall((x) => {
|
||||||
|
assert.strictEqual(x, true);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user