Files
30-seconds-of-code/node_modules/cache-manager-fs-hash/lib/wrap-callback.js
2019-08-20 15:52:05 +02:00

24 lines
498 B
JavaScript

"use strict";
/**
* adds an callback param to the original function
* @param {function} fn
* @returns {function}
*/
module.exports = function wrapCallback(fn) {
return function (...args) {
let cb;
if (typeof args[args.length - 1] === 'function') {
cb = args.pop();
}
const promise = fn.apply(this, args);
if (typeof cb === 'function') {
promise.then(value => setImmediate(cb, null, value), err => setImmediate(cb, err));
}
return promise;
};
};