promisify-node icon indicating copy to clipboard operation
promisify-node copied to clipboard

fs.exists("") returns Promise<undefined>

Open syndicatedshannon opened this issue 7 years ago • 3 comments

var promisifyNode = require("promisify-node");
var fs = require("fs");
var util = require("util");

async function testOne(existsFn) {
    var prom = existsFn("test").catch(e => console.log("error " + e));
    console.log("result " + await prom);
}

async function testBoth() {
    console.log("---builtin");
    var builtin = util.promisify(fs.exists);
    await testOne(builtin);

    console.log("---library");
    var library = promisifyNode(fs).exists;
    await testOne(library);
}

testBoth();

result:

---builtin result false

---library result undefined

syndicatedshannon avatar Jun 26 '18 01:06 syndicatedshannon

This particular method is deprecated: "Stability: 0 - Deprecated: Use fs.stat() or fs.access() instead." But I think we can resolve this in nodegit-promise denodeify method.

diff --git a/src/node-extensions.js b/src/node-extensions.js
index 6ca7518..389e312 100644
--- a/src/node-extensions.js
+++ b/src/node-extensions.js
@@ -17,8 +17,14 @@ Promise.denodeify = function (fn, argumentCount) {
     var args = Array.prototype.slice.call(arguments, 0,
         argumentCount > 0 ? argumentCount : 0);
     return new Promise(function (resolve, reject) {
-      args.push(function (err, res) {
-        if (err) reject(err);
+      args.push(function () {
+        const [err, res] = arguments;
+        // If only one argument is returned (err) then return that value.
+        // fs.exists('', fileExists => console.log(fileExists));
+        if (arguments.length === 1) {
+          resolve(err);
+        }
+        else if (err) reject(err);
         else resolve(res);
       })
       var res = fn.apply(self, args);

tbranyen avatar Jul 16 '18 04:07 tbranyen

Opened a slightly more robust solution in nodegit-promise: https://github.com/nodegit/promise/pull/7

tbranyen avatar Jul 16 '18 17:07 tbranyen

same with fs.stat and fs.access - empty promise

MartinMuzatko avatar Feb 07 '19 14:02 MartinMuzatko