const customPromisifySymbol = Symbol.for('nodejs.util.promisify.custom');
const legacyCustomPromisifySymbol = Symbol.for('util.promisify.custom');
function promisify(func) {
if (typeof func !== 'function') {
throw new TypeError('The "func" argument must be of type function.');
}
const custom =
func[customPromisifySymbol] !== undefined
? func[customPromisifySymbol]
: func[legacyCustomPromisifySymbol];
if (custom !== undefined) {
if (typeof custom !== 'function') {
throw new TypeError('The "util.promisify.custom" argument must be of type function.');
}
return custom;
}
return function (...args) {
return new Promise((resolve, reject) => {
func.call(this, ...args, (err, value) => {
if (err) {
reject(err);
} else {
resolve(value);
}
});
});
};
}
promisify.custom = customPromisifySymbol;
export default promisify;