UNPKG

@specialblend/unpromisify

Version:

convert async functions into callback style functions

20 lines (15 loc) 627 B
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var assert = _interopDefault(require('assert')); function unpromisify(handler) { return (...args) => { assert(args.length, 'unpromisified handler needs at least 1 argument'); const params = args; const callback = args.pop(); assert(typeof callback === 'function', 'provided callback argument must be a function'); handler(...params) .then(data => callback(null, data)) .catch(callback); }; } module.exports = unpromisify;