should functions with a callback be truly asynchronous

40 views
Skip to first unread message

jbaron

unread,
Jun 10, 2010, 2:41:53 AM6/10/10
to nodejs
Hi all,

Are the nodejs functions that have a callback as a last parameter
guaranteed to be truly asynchronous? So for example

fs.readdir("/tmp", function() { sys.debug("A"); })
sys.debug("B");

is guaranteed to always print B A ?

And if this is the case, is it also considered best practice if you
write your own functions which take a callback as an argument to make
sure that they are asynchronous. I have for example some functions
that sometimes go to a database and sometimes get the response from a
cache. In the case of cache, should I use process.nextTick. For
example:

var a = cache.get(key);
if (a)
process.nextTick(function() { cb(null,a) }); // is this a good
solution or just cb(null,a) is also ok
else
db.call(key,function() { cb(null,a) ));

Any insights are appreciated!!!

Peter

Isaac Schlueter

unread,
Jun 10, 2010, 3:03:17 AM6/10/10
to nod...@googlegroups.com
If you're worried about stack depth (like, if there are hundreds and
hundreds of 'em), or if having consistent order of operations is
really important for some reason, then process.nextTick is your
friend.

But, if these things aren't concerns, then there's really no reason to
add the extra complexity.

--i

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

Ben Leslie

unread,
Jun 10, 2010, 3:08:13 AM6/10/10
to nod...@googlegroups.com

I'm not sure if it is guaranteed for the built-in functions, but I
definitely found myself screwing the stack depth if I didn't use
process.nextTick, (if we had tail calls, then maybe it wouldn't be a
problem, I guess process.nextTick, is an explicit tail call), in the
functions I wrote.

I hadn't thought of the implications for the order-of-operations. But
I think you are correct, given the single-threaded, non-interrupted
nature of things, the expectation would be to see 'B, A' always.

Benno

Aaron Heckmann

unread,
Jun 10, 2010, 8:03:38 AM6/10/10
to nod...@googlegroups.com
In situations where the callback might be executed immediately I use
process.nextTick to keep it predictable. I don't like any surprises
like that in my apis.

> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.
>
>


--
Aaron
http://clickdummy.net

jbaron

unread,
Jun 10, 2010, 8:45:46 AM6/10/10
to nodejs
All,

Thanks for the input so far. The main issue I had so far was the
unpredicatbility of the order. And if you do some type of pub/sub, you
are faced with having to define the subscriptions first (which wasn't
always logical in my case) or built some logic to "persist" the
published events until a subscriber was registered.

I didn't have any problems with the stack-dept yet, but this is
certainly also a valid argument. I guess sooner or later would have
been confronted with this issue.

So I guess from now on I'll just add the process.nextTick wrap to make
sure it is truly asynchronous and the make order predictable again.


Peter

caolan

unread,
Jun 10, 2010, 8:57:40 AM6/10/10
to nodejs
I nearly always use nextTick, especially when you might want to bind
more handlers to the callback response. Otherwise they are bound after
the callback has fired, instead of before it (the ordering issue you
already mentioned). Stack depth can occasionally bite you too.

It might be interesting to do some performance comparisons between
using nextTick and just calling the callback synchronously... So far
I've assumed the API niceness of guaranteed async outweighs the
potential performance implications, but I've not really tested the
impact it has.

r...@tinyclouds.org

unread,
Jun 10, 2010, 11:28:32 AM6/10/10
to nod...@googlegroups.com
On Wed, Jun 9, 2010 at 11:41 PM, jbaron <soderbl...@gmail.com> wrote:
> Hi all,
>
> Are the nodejs functions that have a callback as a last parameter
> guaranteed to be truly asynchronous? So for example
>
> fs.readdir("/tmp", function() { sys.debug("A"); })
> sys.debug("B");
>
> is guaranteed to always print B A ?

Yes. The only case I can think of for which that is not true is
array.forEach()...

> And if this is the case, is it also considered best practice if you
> write your own functions which take a callback as an argument to make
> sure that they are asynchronous.

Yes. If it's async most of the time - use nextTick to give users the
consistent interface.

Reply all
Reply to author
Forward
0 new messages