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 = html.get(url);
process.nextTick(function() {callback(big)}); // this seems wrong to me.
big = null;
process. nextTick() is used to schedule a callback function to be invoked in the next iteration of the Event Loop. setImmediate() method is used to execute a function right after the current event loop finishes.
Use await nextTick() to ensure the DOM has updated before the test continues. Functions that might update the DOM (like trigger and setValue ) return nextTick , so you need to await them.
setImmediate() and setTimeout() are similar, but behave in different ways depending on when they are called. setImmediate() is designed to execute a script once the current poll phase completes. setTimeout() schedules a script to be run after a minimum threshold in ms has elapsed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With