• creates a debounced version of the provided function that returns a shared promise.
    unlike conventional debounce, this function reuses and returns the same promise object for all calls that are made within the debouncing interval.
    this means that all callers within this interval will receive the same promise, which will be resolved once wait_time_ms amount of time has passed with no further calls.
    if subsequent calls are made within the debouncing interval, the debounced function will return the same promise as before, further delaying its resolution.
    however, once the debouncing interval has elapsed and the promise is resolved, any new calls to the debounced function will create and return a new promise.

    Type Parameters

    • T extends unknown
    • ARGS extends any[]

    Parameters

    • wait_time_ms: number

      the time interval in milliseconds for debouncing

    • fn: ((...args) => T)

      the function to be debounced

        • (...args): T
        • Parameters

          Returns T

    Returns ((...args) => Promise<T>)

    a function (that takes arguments intended for fn) that returns a promise, which is resolved once wait_time_ms amount of time has passed with no further calls

      • (...args): Promise<T>
      • Parameters

        Returns Promise<T>

    Example

    // assume that `sleep(time_ms: number)` is a function that synchronously creates a delay for `time_ms` number of milliseconds
    const fn = (v: number) => {
    console.log(v)
    return v + 100
    }
    const debounced_fn = debounceAndShare(1000, fn)
    const a = debounced_fn(24) // returns a promise that resolves after 1000ms
    sleep(500)
    const b debounced_fn(42) // returns the same promise as before, but its resolution is delayed by another 1000ms
    // 1000ms later, you should see "42" in your console
    sleep(2000)
    c = debounced_fn(99) // Returns a new promise that resolves after 1000ms
    // 1000ms later, you should see "99" in your console
    // notice that the promises made within the debounce interval are the same pomise objects (ie `a === b`)
    // however, once out of that interval, an entirely new promise is generated (ie `b !== c`)

Generated using TypeDoc