You can’t. Some methods have a sync counterpart, usually, they’re paired functions kind of like foo
and fooSync
.
You could use a control flow library to make async functions look and feel slightly more sync.
I notice that simply leaving off the callback or making a callback null doesn't always make a node function synchronous. What is a good general pattern for making a node method synchronous (assuming the source for the method is available)?
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
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?hl=en
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I notice that simply leaving off the callback or making a callback null doesn't always make a node function synchronous. What is a good general pattern for making a node method synchronous (assuming the source for the method is available)?
Alex, I think in this case I need to. I have about 20 functions to execute, each of which depends on the side-effects of the previous function. I can basically make it synchronous by making what looks like a big 'V'
Your problem is not asynchrony vs synchrony. Please forget these words. Nothing ever happens synchronously on a computer. You've been lied to all your life, sorry.
Your problems is about flow control. Flow control is a set of patterns for scheduling atomic actions. Some actions can be gathered together as a single operation. These are functions in javascript. Some operations need to wait for something else to finish. These are async operations. In some environments, async operations are made to look synchronous using things such as threads, processes and fibers. But they are not synchronous. If you're comfortable with hiding this complexity, you can use python or ruby (or anything else, really)
The problem you are having can be solved by choosing a method of scheduling. In node the following methods exist.
Pipelines : streams and pipe(). These are core core methods. Several stream modules exist.
Callbacks scheduler : async, flowjs, step, etc.
Promises : rsvp, futures, etc.
Transpilers : streamline, iced coffeescript (these are not strictly javascript)
Async/wait : fibers, galaxy, etc. (these need a special version of node)
The names on the right hand side are npm modules.
It is up to you to choose what method you prefer. They all have their pros and cons. My advice is to read up on each at callbackhell.com.
My second advice is to ask people trying to sell you their stuff in which category their stuff belongs.
My third advice is to use async.waterfall from the async library. And this is my favourite way of doing things.
Hope this makes things a bit clearer.
Floby.
Kevin, I guess that the tone of some of these exchanges may surprise you a bit. The explanation is that there have been many sour discussions around this topic in the past (and I've been one of the most active participants). Things are more relaxed these days but people tend to be still a bit edgy. To clarify where I stand: I wrote on of the transpiler solutions (streamline) as well as one of the generator-based libraries (galaxy). I'm also supportive of fibers because it is one of the runtime options behind streamline and this is the one that we chose to release our own product.
My only advice to you is to *not* trust what you will read from a single source like callbackhell.com but to try things out and make up your own mind. Also google around. Look for benchmarks and real-world experiences with the solutions, not just opinions.
Bruno
Thank you very much for all the replies. This has been most enlightening. Based on all of the replies and my situation I would like to explore Promises more. I think I understand the basic idea behind promises but lack specific implementation details. Does anyone have a good example of using Q and Node.js like the readme indicates:
<snip happens>
I get lost with denodify and deferred which the above simplification doesn't address. Any clarification for the newly initiated?
var promise = node.fs.cat(filename, encoding);promise.addCallback(function (data) {body = data;headers = [ [ "Content-Type" , content_type ],[ "Content-Length" , body.length ]];headers.push(["Cache-Control", "public"]);callback();});promise.addErrback(function () {notFound(req, res);});
I would like to bring some experience and history to this conversation.
It seems to me that `yield` instructions block the same way that `wait` blocks with thread or processes. async/wait is more or less threading (cooperative threading).