1'use strict'; 2 3Object.defineProperty(exports, "__esModule", { 4 value: true 5}); 6exports.default = awaitify; 7// conditionally promisify a function. 8// only return a promise if a callback is omitted 9function awaitify(asyncFn, arity = asyncFn.length) { 10 if (!arity) throw new Error('arity is undefined'); 11 function awaitable(...args) { 12 if (typeof args[arity - 1] === 'function') { 13 return asyncFn.apply(this, args); 14 } 15 16 return new Promise((resolve, reject) => { 17 args[arity - 1] = (err, ...cbArgs) => { 18 if (err) return reject(err); 19 resolve(cbArgs.length > 1 ? cbArgs : cbArgs[0]); 20 }; 21 asyncFn.apply(this, args); 22 }); 23 } 24 25 return awaitable; 26} 27module.exports = exports['default'];