diff options
Diffstat (limited to 'app/scripts/lib/nodeify.js')
-rw-r--r-- | app/scripts/lib/nodeify.js | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/app/scripts/lib/nodeify.js b/app/scripts/lib/nodeify.js index 51d89a8fb..9b595d93c 100644 --- a/app/scripts/lib/nodeify.js +++ b/app/scripts/lib/nodeify.js @@ -1,24 +1,18 @@ -module.exports = function (promiseFn) { - return function () { - var args = [] - for (var i = 0; i < arguments.length - 1; i++) { - args.push(arguments[i]) - } - var cb = arguments[arguments.length - 1] - - const nodeified = promiseFn.apply(this, args) +const promiseToCallback = require('promise-to-callback') +const noop = function () {} - if (!nodeified) { - const methodName = String(promiseFn).split('(')[0] - throw new Error(`The ${methodName} did not return a Promise, but was nodeified.`) +module.exports = function nodeify (fn, context) { + return function () { + const args = [].slice.call(arguments) + const lastArg = args[args.length - 1] + const lastArgIsCallback = typeof lastArg === 'function' + let callback + if (lastArgIsCallback) { + callback = lastArg + args.pop() + } else { + callback = noop } - nodeified.then(function (result) { - cb(null, result) - }) - .catch(function (reason) { - cb(reason) - }) - - return nodeified + promiseToCallback(fn.apply(context, args))(callback) } } |