From 0ac49d63d9106b1a4f60e1d55a6a39127b8bd21f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 9 Jul 2019 23:09:25 -0400 Subject: [PATCH] fix: remove left over code, use proper names --- src/promisify.js | 50 +++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/promisify.js b/src/promisify.js index 6830adc40e..f56e8e68bc 100644 --- a/src/promisify.js +++ b/src/promisify.js @@ -17,54 +17,33 @@ module.exports = function (theModule, ignoreKeys) { return fn && fn.constructor && fn.constructor.name === 'AsyncFunction'; } - var parts = []; - function promisifyRecursive(module, key) { + + function promisifyRecursive(module) { if (!module) { return; } - if (key) { - parts.push(key); - } + var keys = Object.keys(module); keys.forEach(function (key) { if (ignoreKeys.includes(key)) { return; } if (isAsyncFunction(module[key])) { - module[key] = wrapIt(module[key], util.callbackify(module[key])); + module[key] = wrapCallback(module[key], util.callbackify(module[key])); } else if (isCallbackedFunction(module[key])) { - module[key] = wrapTwo(module[key], util.promisify(module[key])); + module[key] = wrapPromise(module[key], util.promisify(module[key])); } else if (typeof module[key] === 'object') { - promisifyRecursive(module[key], key); + promisifyRecursive(module[key]); } }); - parts.pop(); } - function wrapTwo(origFn, promiseFn) { - return function wrapper2(...args) { - if (arguments.length && typeof arguments[arguments.length - 1] === 'function') { - return origFn.apply(null, args); - } - - return promiseFn.apply(null, arguments); - }; - } - - function wrapIt(origFn, callbackFn) { - return async function wrapper(...args) { + function wrapCallback(origFn, callbackFn) { + return async function wrapperCallback(...args) { if (arguments.length && typeof arguments[arguments.length - 1] === 'function') { const cb = args.pop(); args.push(function (err, res) { - if (err) { - return cb(err); - } - - // fixes callbackified functions used in async.waterfall - if (res !== undefined) { - return cb(err, res); - } - return cb(err); + return res !== undefined ? cb(err, res) : cb(err); }); return callbackFn.apply(null, args); } @@ -72,6 +51,17 @@ module.exports = function (theModule, ignoreKeys) { }; } + function wrapPromise(origFn, promiseFn) { + return function wrapperPromise(...args) { + if (arguments.length && typeof arguments[arguments.length - 1] === 'function') { + return origFn.apply(null, args); + } + + return promiseFn.apply(null, arguments); + }; + } + + var parts = []; function deprecateRecursive(module, key) { if (!module) { return;