Skip to content

once does not preserve this context #2068

Closed

Description

@jeonghoon11

es-toolkit version

1.51.0

Entry point

es-toolkit/compat

Reproduction

import { once as esToolkitOnce } from 'es-toolkit/compat';
import { once as lodashOnce } from 'lodash';

const esToolkitObject = {
  value: 42,
  getValue: esToolkitOnce(function (this: { value: number }) {
    return this?.value;
  }),
};

const lodashObject = {
  value: 42,
  getValue: lodashOnce(function (this: { value: number }) {
    return this?.value;
  }),
};

console.log(esToolkitObject.getValue()); // undefined
console.log(lodashObject.getValue()); // 42

Current vs. expected behavior

The once function in es-toolkit/compat returns undefined because the wrapped function loses its this context.
For the same input, Lodash preserves the this context and returns 42.
I expect es-toolkit/compat to match Lodash's behavior and return 42.

Validations

  • I searched the existing issues and discussions.
  • I read the Contributing Guidelines.
  • I wrote this description myself.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions