The lodash method once exported as a module to restrict function invocation to a single execution.
npx @tessl/cli install tessl/npm-lodash--once@4.1.0The lodash method _.once exported as a standalone Node.js module. Creates a function that is restricted to invoking a given function once, with subsequent calls returning the cached result from the first invocation.
npm install lodash.onceconst once = require('lodash.once');For ES modules:
import once from 'lodash.once';const once = require('lodash.once');
// Create a function that only runs once
const initialize = once(function() {
console.log('Application initialized');
return { status: 'ready' };
});
// First call executes the function
const result1 = initialize(); // Logs: "Application initialized"
console.log(result1); // { status: 'ready' }
// Subsequent calls return cached result
const result2 = initialize(); // No log output
console.log(result2); // { status: 'ready' } (same reference)
console.log(result1 === result2); // trueCreates a function wrapper that restricts the provided function to execute only once.
/**
* Creates a function that is restricted to invoking `func` once. Repeat calls
* to the function return the value of the first invocation. The `func` is
* invoked with the `this` binding and arguments of the created function.
*
* @param {Function} func The function to restrict.
* @returns {Function} Returns the new restricted function.
* @throws {TypeError} Throws a TypeError if `func` is not a function.
*/
function once(func);Usage Examples:
const once = require('lodash.once');
// Event handler that should only run once
const handleClick = once(function(event) {
console.log('Button clicked for the first time');
// Expensive operation here
return performExpensiveOperation();
});
button.addEventListener('click', handleClick);
// Initialization function
const createConnection = once(function() {
console.log('Creating database connection...');
return new DatabaseConnection();
});
// Multiple calls return the same connection instance
const conn1 = createConnection();
const conn2 = createConnection();
console.log(conn1 === conn2); // true
// Function with parameters - first call's arguments are used
const greet = once(function(name) {
return `Hello, ${name}!`;
});
console.log(greet('Alice')); // "Hello, Alice!"
console.log(greet('Bob')); // "Hello, Alice!" (cached result)
// Preserves `this` context
const obj = {
value: 42,
getValue: once(function() {
return this.value;
})
};
console.log(obj.getValue()); // 42
console.log(obj.getValue()); // 42 (cached)Error Handling:
const once = require('lodash.once');
try {
const invalid = once('not a function');
} catch (error) {
console.log(error instanceof TypeError); // true
console.log(error.message); // "Expected a function"
}Return Value Behavior:
undefined, subsequent calls will also return undefinedundefined (since no result was stored)this binding as the first invocationconst once = require('lodash.once');
// Function that throws an error
const errorFunction = once(function() {
throw new Error('Something went wrong');
});
try {
errorFunction(); // Throws error
} catch (e) {
console.log(e.message); // "Something went wrong"
}
const result = errorFunction(); // Returns undefined (no error thrown)
console.log(result); // undefined
// Function that returns a value before throwing would still cache the value
const mixedFunction = once(function(shouldThrow) {
if (shouldThrow) {
throw new Error('Error on first call');
}
return 'success';
});
console.log(mixedFunction(false)); // 'success'
console.log(mixedFunction(true)); // 'success' (cached, ignores new argument)