Skip to content
Advertisement

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.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement