What is lodash.once?
The lodash.once package is a utility that allows you to create a function that can only be called once. After the first call, subsequent calls to the function will return the result of the first call without executing the function again. This is useful for initializing resources or other actions that should only happen once.
Creating a function that can only be called once
This feature allows you to create a function that will only execute its body once and will return the result of the first execution on subsequent calls.
const once = require('lodash.once');
const initialize = once(() => {
console.log('Initialization complete.');
return 'Initialized';
});
console.log(initialize()); // Output: Initialization complete.
console.log(initialize()); // Output: Initialized (without logging 'Initialization complete.' again)