mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-23 02:10:12 +01:00
Merge pull request #2305 from MetaMask/nodeify
nodeify - allow callback to be optional
This commit is contained in:
commit
4a4338c1f4
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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'))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user