Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: How do I pass arguments to a function using process.nextTick

Tags:

node.js

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;
like image 862
leech Avatar asked Aug 23 '11 22:08

leech


People also ask

What is the difference between process nextTick () and setImmediate ()?

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.

What is await nextTick?

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.

What is difference between setTimeout and setImmediate?

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.


1 Answers

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.

like image 164
Benjamin Atkin Avatar answered Sep 24 '22 02:09

Benjamin Atkin