A way to reduce nested code

83 views
Skip to first unread message

Andi

unread,
Jun 2, 2010, 11:44:47 AM6/2/10
to nodejs
Is there a way to execute an asynchronous call as if it's synchronous,
which is useful in the most cases? Example: http://github.com/christkv/node-mongodb-native
This is the MongoDB driver for node.js and the example code won't do
anything but waiting for the callbacks and then starting the next
asynchronous call.
I have read that there is something like ...

asyncMethod().wait();
nextcall();

Guillermo Rauch

unread,
Jun 2, 2010, 12:10:58 PM6/2/10
to nod...@googlegroups.com
The `coroutines` that we had for a while added a lot of complexity to the codebase, specially when dealing
with events, so they were dropped.
There're several strategies to make async code beautiful and practical (queues, promises are some).

One of our projects is mongoose, a library on top of node-mongodb-native that has exactly this goal (among
many others).

We're probably releasing 0.1 today, with a lot of bug fixes, enhancements, better documentation and specs.

--
Guillermo Rauch
http://devthought.com

Timothy Caswell

unread,
Jun 2, 2010, 6:27:07 PM6/2/10
to nod...@googlegroups.com
I use my Step library, I also have one called Do, and one that I helped tmpvar with called Conductor.  Also there is the Promise library.

They are all four libraries that help mitigate the nesting without resorting to blocking 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.


tjholowaychuk

unread,
Jun 3, 2010, 9:56:21 AM6/3/10
to nodejs
Personally I just recommend keeping your public apis abstracted and
short/sweet. I usually never nest more than 3 three times

Andi

unread,
Jun 3, 2010, 12:33:53 PM6/3/10
to nodejs
Thanks, I will take a deeper look. I will use the one that comes
closest to:

var a, b = asyncCall(param);
useResults ...

instead of:
asyncCall(param, function (a, b) {
useResults ...
});
Reply all
Reply to author
Forward
0 new messages