I'm trying to make a web crawler to fetch products from certain sites to reduce my memory usage (I've got a memory leak somewhere i haven't found). So I'm trying to send arguments to a callback asynchronously in order to terminate the current context.
This is where I'm at:
var big = html.get(url);
callback(big);
big = null; // this isn't going to get erased until after big is done is it?
This is what I've tried:
var big = htm开发者_StackOverflow中文版l.get(url);
process.nextTick(function() {callback(big)}); // this seems wrong to me.
big = null;
process.nextTick() doesn't support passing arguments like setTimeout() does. You will just need to make a function that passes the arguments.
So instead of:
setTimeout(myfunc, 0, 'arg1', 'arg2')
Use this:
process.nextTick(function() { myfunc('arg1', 'arg2') })
You can also use a function to build your function.
Finally, setTimeout doesn't support specifying the object that the function gets called on, so it's limited in its usefulness.
var big = html.get(url);
process.nextTick(function() {callback(big); big = null; });
If this style isn't obvious to you, then you should study closures more.
(incidentally, setting big to null is probably unnecessary here; once the callback finishes, big is going out of scope.)
Edit: In honor of the six-year anniversary of this question, here is an ECMA-6 version of the same thing:
const big = html.get(url);
process.nextTick(() => callback(big));
Although process.nextTick() doesn't directly support passing arguments, there is a shortcut that achieves the same thing.
var big = html.get(url);
process.nextTick(callback.bind(null, big));
Note that this isn't functionally any different from the explicit closure answers already given (minus manually setting big = null;
instead of letting it go out of scope on its own), but I feel it makes the code a little easier to read.
bind()
is actually a much more powerful tool than is shown here; read more (including some examples) at the MDN documentation.
Details: The first parameter to bind()
is the value which will be assigned to this
during the execution of callback()
, which is assigned to null
here since it looks like it wouldn't be used in this case. Any additional parameters (only big
in this case) will be passed as arguments to callback
when it is called later, via a technique called "partial function application".
精彩评论