Syntax, Parameter and Return Value
Syntax:
_.after(n, func)
Parameters:
-
n (number): The number of calls before func is executed.
- func (Function): The function to restrict.
Return Value:
(Function) - Returns the new restricted function.
Examples
Executing After Multiple Events:
JavaScript
var _ = require('lodash');
var save = () => console.log('Saving data...');
var saveAfterThreeCalls = _.after(3, save);
saveAfterThreeCalls(); // Not yet called 3 times.
saveAfterThreeCalls(); // Not yet called 3 times.
saveAfterThreeCalls();

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Saving data...'
Demonstrates delaying function execution until it's been called three times.
Handling Multiple Async Operations:
JavaScript
var completeOperation = _.after(2, () => console.log('Operations completed.'));
// Simulating asynchronous operations
setTimeout(completeOperation, 100); // First async operation completed.
setTimeout(completeOperation, 200);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Operations completed.'
Shows executing a function after completing multiple asynchronous operations.
Loading Multiple Resources:
JavaScript
var resourcesLoaded = _.after(5, () => console.log('All resources loaded.'));
// Assuming each 'loadResource' invocation is triggered after a resource is loaded.
for (let i = 0; i < 5; i++) {
loadResource(resourcesLoaded);
}

You can also try this code with Online Javascript Compiler
Run Code
Output after 5th resource:
'All resources loaded.'
An example of using _.after() to execute a callback after loading multiple resources.
Batch Processing Completion:
JavaScript
var processBatch = _.after(10, () => console.log('Batch processing complete.'));
for (let i = 0; i < 10; i++) {
processItem(i, processBatch);
}

You can also try this code with Online Javascript Compiler
Run Code
Output after 10th item:
'Batch processing complete.'
Demonstrates using _.after() to notify when batch processing is complete.
Frequently Asked Questions
What happens if _.after() is called with n less than or equal to 1?
If n is less than or equal to 1, the function func will execute immediately upon its first call.
Can _.after() be reset or does it persist its count state?
The count state in _.after() persists; it doesn't reset. Once the function has been executed, subsequent calls will always execute the function.
Is _.after() suitable for managing event handlers?
Yes, _.after() can be effective for managing event handlers, especially when an action should only be triggered after an event has occurred a specific number of times.
Conclusion
Lodash's _.after() method is a practical tool for controlling the execution of functions based on the number of times they are called. It is particularly useful in managing sequential operations, batch processes, and coordinating actions in asynchronous workflows.
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.