or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--once

The lodash method once exported as a module to restrict function invocation to a single execution.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.once@4.1.x

To install, run

npx @tessl/cli install tessl/npm-lodash--once@4.1.0

index.mddocs/

lodash.once

The 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.

Package Information

  • Package Name: lodash.once
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.once

Core Imports

const once = require('lodash.once');

For ES modules:

import once from 'lodash.once';

Basic Usage

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); // true

Capabilities

Function Restriction

Creates 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:

  • If the original function returns undefined, subsequent calls will also return undefined
  • If the original function throws an error, that error is thrown on the first call, and subsequent calls return undefined (since no result was stored)
  • The returned function maintains the same this binding as the first invocation
  • After the first invocation, the original function reference is cleared for memory efficiency
const 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)