如何在异步函数上使用 debounce?

How to use debounce on async function?

如何在 async 函数上使用 debounce?我的 vue-app 中有一个方法,它从 API 中获取数据,它连续调用 API,我想避免。

这是我的方法:

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

我之前已经安装了 lodash 那么我该如何实现呢?

Lodash 的 debounce 函数接受一个函数,等待时间和 returns 一个函数。

所以这样做:

methods: {
  getAlbums: _.debounce(async function() {
    const response = await AlbumService.fetchAlbums();
    this.albums = response.data.albums;
  }, 1000);
}