'Lodash debounce async/await

I'm trying to add debounce to my application before I do an api call. However when I introduce debouce, it seems like my await is ignored and the function calls due to missing values

export default class App extends Component {
  state = {
    search: "Cats",
    results: []
  };

  async search(text) {
    const giphy = {
      baseURL: "https://api.giphy.com/v1/gifs/search",
      apiKey: "0UTRbFtkMxAplrohufYco5IY74U8hOes",
      tag: text
    };

    let giphyURL = encodeURI(
      giphy.baseURL + "?api_key=" + giphy.apiKey + "&q=" + giphy.tag
    );

    let data = await fetch(giphyURL);
    return data.json();
  }

  async componentDidMount() {
    // get default search
    this.onSearch(this.state.text);
  }

  setSearch = e => {
    this.setState({ search: e.target.value });

    debounce(() => this.onSearch(this.state.search), 100);
  };

  async onSearch(text) {
    console.log("text:", text);
    try {
      let response = await this.search(this.state.search);
      console.log("data:", response.data);
      // console.log(data.results);

      let data = response.data.reduce((t, { title, id, images }) => {
        t.push({ title, id, url: images.downsized_medium.url });
        return t;
      }, []);
      this.setState({ results: data });
    } catch (e) {
      console.error("Failed Fetch", e.toString());
    }
  }

  render() {
    return (
      <main className="app">
        <Header>This is my Gif Search App</Header>
        <nav className="navbar">
          <SearchBox onSearch={this.setSearch} value={this.state.search} />
        </nav>
        <aside className="sidebar">Sidebar Bar</aside>
        <section className="results">
          <Results results={this.state.results} />
        </section>
        <footer className="footer">
          <p className="footer-text">Copyright @funssies 2019</p>
        </footer>
      </main>
    );
  }
}

Error: in the setSearch method, I wrapped the call to get data in debounce but nothing happens.



Solution 1:[1]

I think I figured it out. Deboune returns a function. Then I have to invoke that function

For example:

let myFunc = debounce(this.someFunction,100)

// call debounced function 
myFunc()

I changed my function to this:

  delayedSearch = debounce(this.onSearch, 1500);

  setSearch = e => {
    this.setState({ search: e.target.value });

    this.delayedSearch(this.state.search);
  };

Found help here: lodash debounce not working in anonymous function

Solution 2:[2]

onSearch is an async function. The argument function for debounce also has to be async. Yep, like Batman pointed out.

const debouncedOnSearch = debounce(async () => {
         this.onSearch(this.state.search)
    }, 100);
setSearch = e => {
    this.setState({ search: e.target.value });
    debouncedOnSearch();

  };

This should work. Another similar question I found.

Hope this helped.

Solution 3:[3]

Here is a simple async version (works with debounce npm)

export default function MyComponent() {
    const [searchTerm, setSearchTerm] = useState('');
    const launchSearch = async (searchValue: string): Promise<any> => {
        console.log(searchValue);
    };

    const delayedSearch = useCallback(
        debounce(async (searchTerm) => {
            await launchSearch(searchTerm);
        }, 2000),
        []
    );
    useEffect(() => {
        if (searchTerm.length >= 2) {
            delayedSearch(searchTerm);
        }
    }, [searchTerm]);

    ... 
}

Solution 4:[4]

In addition to the answer above, if your throttled/debounced handler uses props or state, like this:

const { fetcherFunctionFromProps } = props;

const eventHandler = async () => {
  const resp = await fetcherFunctionFromProps();
};    

const debouncedEventHandler = useMemo(
  () => throttle(eventHandler, 300)
), [fetcherFunctionFromProps]);

And it doesn't work,
you can refactor it to the following:

const { fetcherFunctionFromProps } = props;

const eventHandler = async (fetcher) => {
  const resp = await fetcher();
};

const debouncedEventHandler = useMemo(() => throttle(eventHandler, 300), []);
...
<Component onClick={() => debouncedEventHandler(fetcherFunctionFromProps)}>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Stephane
Solution 4 Vlad Ryabinin