Book Image

Lo-Dash Essentials

By : Adam Boduch
Book Image

Lo-Dash Essentials

By: Adam Boduch

Overview of this book

Table of Contents (15 chapters)
Lo-Dash Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Function constraints


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.

Limiting call counts

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...