question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature request - debounce asyncComputed

See original GitHub issue

It would be nice to be able to define a computed function which waits for 0 or more milliseconds before issuing the compute request, and provides various options for what happens if subsequent requests arrive during that delay.

I’m using asyncComputed currently with fetch, but I want to throttle the number of fetch requests being made, and show a “working…” temporary output or something.

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
foxbenjaminfoxcommented, Dec 9, 2016

That does indeed sound like a useful thing to be able to do.

I don’t think you need any particular help from this plugin to do it though. Already, you can write something like this, using any promise-based debounce, for instance as provided by the debounce-promise package:

const debounce = require('debounce-promise')

new Vue({
  asyncComputed: {
    myProperty: debounce(function () {
      // The function body, which returns a promise
    }, 100)
  }
}

Debouncing the function passed to vue-async-computed does not require any particular changes to the plugin.

On the other hand, I don’t know off the top of my head of a simple way to do what you want with regards to showing the placeholder value while the debouncing is going on. It is true that it seems like that could be a useful feature. That said, it’s more a feature for a debouncing library than for this plugin directly. It’s more of a generic debouncing feature than anything specifically to do with Vue.

That said, I’m not against adding that sort of thing if it would simplify matters, although perhaps I’d add it in a separate library as most users of vue-async-computed won’t need it and it would be a shame to bulk vue-async-computed up with things not directly related to Vue.

1reaction
geekytimecommented, Jun 8, 2019

I think the debounced function is failing to react because Vue cannot “hear” the the accessors to my other computed values from inside the debounce function wrapper. If I create the debounced function outside the Vue instance, and pass any computed values into the wrapper, it works:

const fetchItems = debounce(function () {
  const response = await api.get({
    url: '/some/url',
    params: this.params
  })
  return response.results
})

const vm = new Vue({
  data: {
    showing: 'all',
    name: '',
  },
  computed: {
    params () {
      return {
        showing: this.showing,
        name: this.name
      }
    }
  },
  asyncComputed: {
    items: {
      default: [],
      lazy: true,
      async get () {
        return fetchItems(this.params)
      }
    }
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to debounce async validator in Angular 4 with RxJS ...
Here, I'm converting existing observable to promise using toPromise() operator of RxJS. Factory function is used because we need a separate ...
Read more >
ng5-async-debounce - npm Package Health Analysis - Snyk
The npm package ng5-async-debounce was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was ...
Read more >
How to create a real-time search using debounce in react.js
This article shows how to create a real-time search functionality using debouncing in your react app where the search results update in ...
Read more >
Testing Components - Ember Guides
Imagine you have a typeahead component that uses Ember.run.debounce to limit requests to the server, and you want to verify that results are...
Read more >
Improve performance for Vue InstantSearch - Algolia
Debouncing is a way to limit the number of requests and avoid processing non-necessary ones by avoiding sending requests before a timeout.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found