2017-07-13 00:10:52 +02:00
|
|
|
const promiseToCallback = require('promise-to-callback')
|
2017-10-06 21:36:08 +02:00
|
|
|
const noop = function(){}
|
2016-11-28 21:43:44 +01:00
|
|
|
|
2017-08-04 03:20:01 +02:00
|
|
|
module.exports = function nodeify (fn, context) {
|
2017-07-13 00:06:49 +02:00
|
|
|
return function(){
|
|
|
|
const args = [].slice.call(arguments)
|
2017-10-06 22:02:34 +02:00
|
|
|
const lastArg = args[args.length - 1]
|
2017-10-06 21:36:08 +02:00
|
|
|
const lastArgIsCallback = typeof lastArg === 'function'
|
|
|
|
let callback
|
|
|
|
if (lastArgIsCallback) {
|
|
|
|
callback = lastArg
|
|
|
|
args.pop()
|
|
|
|
} else {
|
|
|
|
callback = noop
|
|
|
|
}
|
2017-07-13 00:06:49 +02:00
|
|
|
promiseToCallback(fn.apply(context, args))(callback)
|
2016-11-28 21:43:44 +01:00
|
|
|
}
|
|
|
|
}
|