How to use debounce on async function? [duplicate]

2020-06-23 07:27发布

How can I use debounce on an async function? I have a method within my vue-app which reveives data from an API which calls the API continuosly which I want to avoid.

Here is my method:

methods: {
    async getAlbums () {
     const response = await AlbumService.fetchAlbums()
     this.albums = response.data.albums
    } 
}

I've installed lodash previously so how can I achieve that?

1条回答
乱世女痞
2楼-- · 2020-06-23 07:40

Lodash's debounce function takes in a function , time to wait and returns a function.

So do it like this:

methods: {
  getAlbums: _.debounce(async function() {
    const response = await AlbumService.fetchAlbums();
    this.albums = response.data.albums;
  }, 1000);
}
查看更多
登录 后发表回答