Similar to decorating functions with new behavior are the constraints imposed on functions. This impacts when and how often the function can be called. Function constraints also control how values returned by calling a function are cached. Lo-Dash has functions that deal with each of these scenarios.
There are two Lo-Dash functions that deal with counting the number of times a given function is called. The after()
function will execute a callback after the composed function is called a given number of times. The once()
function constrains the given function to being called only once. Let's look at after()
and see how it works:
function work(value) { progress(); } function reportProgress() { console.log(++complete + '%'); progress = complete < 100 ? _.after(0.01 * collection.length, reportProgress) : _.noop; } var complete = 0, collection = _.range(9999999), progress = _.noop; reportProgress(); _.forEach...