最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Debounce and return - Stack Overflow

programmeradmin0浏览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?

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?

Share Improve this question asked Oct 16, 2018 at 8:14 Igino BoffaIgino Boffa 7363 gold badges10 silver badges23 bronze badges 11
  • 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 Commented Oct 16, 2018 at 8: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 Commented Oct 16, 2018 at 8:16
  • I don't understand this. Promises only resolve Once. Debounce prevents something from happening multiple times too fast after eachother. – Shilly Commented Oct 16, 2018 at 8:16
  • link=debounce – Igino Boffa Commented Oct 16, 2018 at 8:17
  • 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 Commented Oct 16, 2018 at 8:22
 |  Show 6 more ments

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论