Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
syndicatedshannon opened this issue Jun 26, 2018 · 3 comments
Open

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

syndicatedshannon opened this issue Jun 26, 2018 · 3 comments

Comments

@syndicatedshannon
Copy link

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

@tbranyen
Copy link
Member

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
Copy link
Member

Opened a slightly more robust solution in nodegit-promise: nodegit/promise#7

@MartinMuzatko
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants