Syntax, Parameter and Return Value
Syntax:
_.once(func)
Parameters:
func (Function): The function to restrict to a single call.
Return Value:
(Function) - Returns the new restricted function.
Examples
Single Initialization Function:
JavaScript
var _ = require('lodash');
function initialize() {
console.log('Initialization completed');
}
var initializeOnce = _.once(initialize);
initializeOnce(); // Output: 'Initialization completed'
initializeOnce(); // No output on subsequent calls

You can also try this code with Online Javascript Compiler
Run Code
Demonstrates ensuring an initialization function is only executed once.
One-Time Event Handler:
JavaScript
var button = document.getElementById('myButton');
var handleClickOnce = _.once(() => console.log('Button clicked'));
button.addEventListener('click', handleClickOnce);

You can also try this code with Online Javascript Compiler
Run Code
// 'Button clicked' is logged only on the first click
Shows using _.once() for an event handler to respond to the first event only.
Single Execution in Asynchronous Operations:
JavaScript
function fetchData(callback) {
// Simulate fetching data
setTimeout(callback, 1000);
}
var logDataOnce = _.once(() => console.log('Data fetched'));
fetchData(logDataOnce);
fetchData(logDataOnce);

You can also try this code with Online Javascript Compiler
Run Code
// 'Data fetched' is logged only once, even though fetchData is called twice
An example of using _.once() in asynchronous operations to avoid duplicate actions.
Preventing Duplicate Submissions:
JavaScript
function submitForm() {
console.log('Form submitted');
}
var submitFormOnce = _.once(submitForm);
submitFormOnce();
submitFormOnce(); // Subsequent calls do nothing

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Form submitted'
Demonstrates using _.once() to prevent duplicate form submissions.
Frequently Asked Questions
Can _.once() be reset to allow the function to run again?
No, once _.once() is applied to a function, it cannot be reset. The function will always return the result of the first call.
Does _.once() affect the original function?
No, _.once() does not alter the original function. It creates a new function with the once-only restriction.
Is _.once() suitable for functions with side effects?
Yes, _.once() is particularly suitable for functions with side effects that should only occur once, like initialization routines or API calls.
Conclusion
Lodash's _.once() method is a simple yet powerful tool for ensuring that a function is executed only a single time. It's especially useful for managing one-time operations, initialization tasks, and preventing duplicate actions in event-driven programming.
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.