aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrankie <frankie.diamond@gmail.com>2017-10-07 05:08:58 +0800
committerGitHub <noreply@github.com>2017-10-07 05:08:58 +0800
commit4a4338c1f4669f621fc39d34b06a77f247c7ce65 (patch)
tree71cd7b38a3040ea6a05a2b632648f5931dcb8dc4
parentfa11bbf99673bb6b2dd4ad5f291d63ff1358fda6 (diff)
parent0b6dab384fe1153f16575e662fd61dbade3b9d64 (diff)
downloadtangerine-wallet-browser-4a4338c1f4669f621fc39d34b06a77f247c7ce65.tar.gz
tangerine-wallet-browser-4a4338c1f4669f621fc39d34b06a77f247c7ce65.tar.zst
tangerine-wallet-browser-4a4338c1f4669f621fc39d34b06a77f247c7ce65.zip
Merge pull request #2305 from MetaMask/nodeify
nodeify - allow callback to be optional
-rw-r--r--app/scripts/lib/nodeify.js12
-rw-r--r--test/unit/nodeify-test.js7
2 files changed, 13 insertions, 6 deletions
diff --git a/app/scripts/lib/nodeify.js b/app/scripts/lib/nodeify.js
index 832d6c6d3..d24e92206 100644
--- a/app/scripts/lib/nodeify.js
+++ b/app/scripts/lib/nodeify.js
@@ -1,10 +1,18 @@
const promiseToCallback = require('promise-to-callback')
+const noop = function(){}
module.exports = function nodeify (fn, context) {
return function(){
const args = [].slice.call(arguments)
- const callback = args.pop()
- if (typeof callback !== 'function') throw new Error('callback is not a function')
+ const lastArg = args[args.length - 1]
+ const lastArgIsCallback = typeof lastArg === 'function'
+ let callback
+ if (lastArgIsCallback) {
+ callback = lastArg
+ args.pop()
+ } else {
+ callback = noop
+ }
promiseToCallback(fn.apply(context, args))(callback)
}
}
diff --git a/test/unit/nodeify-test.js b/test/unit/nodeify-test.js
index 537dae605..c7b127889 100644
--- a/test/unit/nodeify-test.js
+++ b/test/unit/nodeify-test.js
@@ -18,14 +18,13 @@ describe('nodeify', function () {
})
})
- it('should throw if the last argument is not a function', function (done) {
+ it('should allow the last argument to not be a function', function (done) {
const nodified = nodeify(obj.promiseFunc, obj)
try {
nodified('baz')
- done(new Error('should have thrown if the last argument is not a function'))
- } catch (err) {
- assert.equal(err.message, 'callback is not a function')
done()
+ } catch (err) {
+ done(new Error('should not have thrown if the last argument is not a function'))
}
})
})