Syntax, Parameter and Return Value
Syntax:
_.before(n, func)
Parameters:
-
n (number): The number of calls at which func is no longer invoked.
- func (Function): The function to restrict.
Return Value:
(Function) - Returns the new restricted function.
4 Examples
Limiting Button Click Handler:
JavaScript
var _ = require('lodash');
function sendAnalytics() {
console.log('Analytics sent');
}
var sendOnce = _.before(2, sendAnalytics)
// Simulating button clicks
sendOnce();
sendOnce(); // No output (second call is the limit)
sendOnce(); // No output (ignored)

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Analytics sent'
Demonstrates limiting a function to be executed only once.
Restricting API Calls:
JavaScript
function fetchData() {
console.log('Fetching data...');
}
var fetchDataTwice = _.before(3, fetchData);
// Simulating API calls
fetchDataTwice();
fetchDataTwice();
fetchDataTwice(); // No output (third call is the limit)

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Fetching data...'
'Fetching data...'
Shows limiting an API call function to execute only twice.
Managing Event Listeners:
JavaScript
function resizeHandler() {
console.log('Window resized');
}
var handleResizeOnce = _.before(2, resizeHandler);
window.addEventListener('resize', handleResizeOnce);

You can also try this code with Online Javascript Compiler
Run Code
// The 'resizeHandler' will be triggered only once, regardless of how many times the event occurs.
An example of using _.before() to limit an event handler's execution.
Controlling Repetitive Operations:
JavaScript
function repetitiveTask() {
console.log('Task executed');
}
var executeTwice = _.before(3, repetitiveTask);
executeTwice();
executeTwice();
executeTwice(); // No output (third call is the limit)

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Task executed'
'Task executed'
Demonstrates using _.before() to control the execution of a repetitive task.
Frequently Asked Questions
What happens if n is set to 1 in _.before()?
If n is set to 1, the function func will only execute once. Subsequent calls to the created function will return the result of the first (and only) execution.
Can the limit n be changed after _.before() is called?
No, once _.before() is called, the limit n is fixed, and the behavior of the returned function cannot be altered.
Is _.before() useful for debouncing or throttling?
While _.before() limits function executions, it's different from debouncing or throttling. For controlling the rate of function execution over time, use _.debounce() or _.throttle() instead.
Conclusion
Lodash's _.before() method is a valuable tool for controlling the number of times a function can be executed. It is particularly useful in scenarios where limiting function invocation is necessary to avoid excessive calls or manage resource usage efficiently.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.