Skip to content

debounce's options.trailing being unexpectedly set #4973

Closed
@k-funk

Description

@k-funk

I found debounce setting trailing to `false unexpectedly in this code. The react is not part of the problem, but I'm including it for you to understand why I destructured the object in the first place:

import { useState, useRef, useEffect } from 'react'
import { debounce, DebouncedFunc, DebounceSettings } from 'lodash'

export default function useDebounce(value, delay, { leading, trailing, maxWait }): {
  useEffect(() => {
    const dbFn = debounce(setDebounced, delay, { leading, trailing, maxWait })
    ...
  }, [delay, leading, maxWait, trailing])

What ends up happening here is that trailing is getting set to false because of https://github.com/lodash/lodash/blob/master/debounce.js#L89

trailing = true
if (isObject(options)) {
  trailing = 'trailing' in options ? !!options.trailing : trailing
}

I expected that if I didn't pass a boolean to trailing that the default would be used. However { trailing: undefined } is equivalent to { trailing: false } which happens sneakily when you use object-shorthand.

Could lodash instead use trailing = options.trailing !== undefined ? !!options.trailing : trailing, or equivalent vs the in operator?

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue bankruptcyClosing the issue/PR to start fresh

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions