promisify-node
promisify-node copied to clipboard
fs.exists("") returns Promise<undefined>
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
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);
Opened a slightly more robust solution in nodegit-promise: https://github.com/nodegit/promise/pull/7
same with fs.stat and fs.access - empty promise