I am writing a web crawler for reddit which only allows 1 request every 2 seconds and would like to use recursion to make the requests. After the series of get requests for 1 persons comments are complete, I would like to emit to show completion and call the comments again on the next username in a message queue. The problem is whenever I am more than one level deep I get a “TypeError: Object # has no method ’emit'”.
I am also open for better ways to do this, I have just started learning node and am sure there are much better ways to accomplish this.
I have posted my code in the gist below…any help would be awesome!
The issue is with
thiswhich is a keyword (not free variable) that evaluates to the “receiver” for the function call.For instance, given:
Then inside
fn,thisevaluates to whatxevaluated to (and thusx.fn() === x). In the codegrabCommentsis being invoked with no receiver, in which casethisreverts to the global object. You may be interested inFunction.call/apply, or just simplify the code to handle recursion with a nested function so thatselfcan remain bound-to in scope.(In JavaScript, methods are not bound to objects: they are merely functions shoved into properties, so the receiver is paramount in determining
thisfor the executiong function.)See also:
Happy coding.