Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How should I go about forcing synchronicity in node.js?

How should I go about forcing synchronicity in node.js?

Problem

I know that asynchronicity is half the point of node.js and that it's really great and efficient. However in some situations this programming pattern is just overkill and difficult. For instance, what if you wanted to write a script that takes some user input and uses it to create a MySQL row and saves it (I've just done this). It's really annoying to have to create a separate function for every tiny step of the program and then have them all call each other in a row.

So how should such situations instead be handled? I tried out step, but it won't work with coffee-script since it requires functions to be declared in the classic manner (function foo() {/* Bar */}). Coffee-script instead creates anonymous functions and assigns them to local variables (var foo = function() {/* Bar */})

I'm thinking it would just be simpler to mix up a method to get the process to wait for a callback to run. Arguments for asynchronicity aside, how would I go about doing this?

Problem courtesy of: Hubro

Solution

Try https://github.com/caolan/async

The list of "Most Depended On" packages on http://search.npmjs.org/ provides insights into how acclaimed libraries are within the node community. async is in 7th place (depended on by 207 packages)...

underscore    448
coffee-script 341
request       279
express       268
optimist      228
connect       207
async         207
colors        199
uglify-js     146
socket.io     140
redis         128
jade          117
jsdom         102
commander     101
mime           96

Avoid libraries that "do not work" with Coffeescript; they are trouble.

Solution courtesy of: pyrotechnick

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How should I go about forcing synchronicity in node.js?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×