Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am fairly new to NodeJS and I'm having a hard time wrapping my head around the asynchronous nature of functions when I really want one call to follow the other in a synchronized way.

Say for instance I am calling web service methods. Every method has a callback function. But what if I want to call 10 of them in a specific sequence? Do I really need to chain / embed all the functions in the first callback? What if the second time the chain is slightly different?

Are there any elegant patterns to deal with situations where you actually do want things to be synchronous?

Or should I really let go of this kind of programming get used to a different way of thinking? If that's true maybe someone can point me to a clean example of a longer chain of functions using callbacks...

share|improve this question

migrated from stackoverflow.com Dec 14 '11 at 5:59

This question came from our site for professional and enthusiast programmers.

    
10 request in a specific sequence. Are they independent of eachother? –  Jonas Dec 12 '11 at 21:35
    
And note: Linked in a chain doesn't mean it's not async unless everything is linked in that chain. –  Erik Reppen Feb 22 '13 at 22:02
add comment

1 Answer 1

up vote 5 down vote accepted

People usually use Step or Async for that. Check them out, there are a lot of nice tutorials about them, you'll see they come "to the resque:"

http://dailyjs.com/2011/11/14/popular-control-flow/
http://dailyjs.com/2011/11/07/code-review/
http://howtonode.org/step-of-conductor

Also check these 2 answers:

What is the best control flow module for node.js?
Node.js: What techniques are there for writing clean, simple callback code?

share|improve this answer
    
Brilliant! Thanks. –  0x80 Dec 13 '11 at 15:22
    
Glad I could help! –  alessioalex Dec 13 '11 at 15:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.