admin管理员组文章数量:1415137
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
2 Answers
Reset to default 7You 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.
本文标签: javascriptDebounce and returnStack Overflow
版权声明:本文标题:javascript - Debounce and return - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745219906a2648346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论