Reputation: 308
Is there any way to do something after each functions provided to Lodash function _.flow
?
I want to log each consuming time of the functions provided to _.flow
.
For example, for the following code,
_.flow([
funcA,
funcB
])
I would like to log funcA
and funcB
's consuming times like:
funcA spent 1.21s.
funcB spent 0.45s.
Upvotes: 0
Views: 125
Reputation: 308
Thanks Paul Rooney, I implemented it:
function injectTimeLogger (logger, funcs) {
return funcs.map((func, i) => (...params) => {
const funcName = func.name;
const startTime = Date.now();
const rst = func(...params);
const spentTime = (Date.now() - startTime) / 1000;
logger.info(`func[${i + 1}/${funcs.length}][${funcName}] spent ${spentTime}s`);
return rst;
});
}
const logger = {
info: console.log
}
_.flow(injectTimeLogger(logger, [
funcA,
funcB
]))
Upvotes: 2