问题
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:
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);
}
来源:https://stackoverflow.com/questions/50837291/how-to-use-debounce-on-async-function