0

I'm having an issue with this piece of code:

function aFunction(){
  ....
    var deferred = $q.defer();
    debounce(function () {
       deferred.resolve(service.subscribe0(data));
    }, 350);
  return deferred.promise;
}

The returned promise is never resolved. Debounce function is a 3rd party function with a lot of downloads from NPM, so I can be sure it works.

Can it be because the return statement "removes" the scope of the function? How can I avoid this and resolve the promise?

Igino Boffa
  • 686
  • 1
  • 9
  • 21
  • 1
    `so I can be sure it works` - but can you be sure you are using it correctly? can we? how about a link to the npm – Jaromanda X Oct 16 '18 at 08:16
  • if this is the lodash debounce then it returns the debounced function but you're never calling it or returning it. The promise can only be resolved by invoking the debounced function – apokryfos Oct 16 '18 at 08:16
  • I don't understand this. Promises only resolve Once. Debounce prevents something from happening multiple times too fast after eachother. – Shilly Oct 16 '18 at 08:16
  • [link=debounce](https://www.npmjs.com/package/debounce) – Igino Boffa Oct 16 '18 at 08:17
  • well, that takes at least two arguments for a start – Jaromanda X Oct 16 '18 at 08:17
  • Jaromada, it takes 2 arguments: the function and the time. It can happen that the function is called more than once. In my application it is called every time a grid is scrolled – Igino Boffa Oct 16 '18 at 08:18
  • yes ... but debounce returns a function, to be called and debounced - but you discard the debounced function, so it never gets called - as per the answer below – Jaromanda X Oct 16 '18 at 08:19
  • `it is called every time a grid is scrolled` - what funciton is called every time the grid is scrolled? that's not very clear (in fact it's not in the code you put in the question) – Jaromanda X Oct 16 '18 at 08:21
  • @IginoBoffa that is not true, that function you debounce is called exactly 0 times. There is no possible way you can call that function from the code you shared. It's not even assigned to a variable let alone called it. – apokryfos Oct 16 '18 at 08:21
  • 1
    Even then this won't prevent the grid from triggering more often. You have to debounce aFunction so the scrolling the grid will not trigger hundreds of ajax calls. As written, you debounce the promise resolution, which will only happens once to begin with. Each call to aFunction will still create a new debounced function, seperate from the others and hence will do the data call. – Shilly Oct 16 '18 at 08:22
  • You're probably looking for the solution provided here: https://stackoverflow.com/a/35228455/1385429 – Christiaan Westerbeek Nov 26 '18 at 20:33

2 Answers2

7

You misunderstand what debounce() does.

debounce() is a function that accepts a function, and returns a function. The returned function will only call the passed callback after N milliseconds of silence (that is, if you call the debounced function very quickly in sequence, only the last call will take effect, after the time elapses).

debounce() itself doesn't call the function you pass it. So, deferred.resolve() never gets called.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

I would expect something like:

const getData = data => Promise.resolve( service.subscribe0( data ));
grid.addEventListener( 'scroll', debounce( getData, 350 ));

We want the grid to update itsself on scroll, but debounce it so it won't flood the service with calls. So we have to debounce the function tied to the scrolling instead of the data call, since there's no link between two different data calls.

Shilly
  • 8,511
  • 1
  • 18
  • 24