Thursday 30 November 2017

Some scope help.

I've inherited some node.js code and am still getting used to it. I'm having some trouble with variable scoping and have made a very small example to show where the problem lies.var RateLimiter = require('limiter').RateLimiter; var array = [0,1,2]; var limiter = new RateLimiter(1, 5000); for (var i = 0; i < array.length; i++) { console.log("i at for scope: " + i); limiter.removeTokens(1, function(){ console.log("i at removeTokens scope: " + i); }) } The output I get isi at for scope: 0 i at for scope: 1 i at for scope: 2 i at removeTokens scope: 3 i at removeTokens scope: 3 i at removeTokens scope: 3 So it seems i has finished incrementing before the log statements begin. I've been messing around with wrapping in functions and bind statements but I can't seem to solve it while keeping the use of the ratelimiter. Any help would be much appreciated.

Submitted November 30, 2017 at 09:36PM by anonmarmot

No comments:

Post a Comment