async.waterfall([ function(callback){ callback(null, 'one', 'two'); }, function(arg1, arg2, callback){ callback(null, 'three'); }, function(arg1, callback){ // arg1 now equals 'three' callback(null, 'done'); } ], function (err, result) { // result now equals 'done' });
Q.call(step1) .then(step2) .then(step3) .then(step4) .then(function (value4) { // Do something with value4 }, function (error) { // Handle any error from step1 through step4 }) .end();
There are even far more libraries just like this, and even yet more
solutions approaching the problem from different .
This is the usual experience, "node.js oh so great" --- a day later,
"OMG I can't handle all that callbacks, statemachine logics etc.". One
Week later: yet another syncronisation library has been submitted to
teh repository.
> --
> 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
On Sunday, July 1, 2012 2:55:05 PM UTC-4, Mariusz Nowak wrote:
n promises approach asynchronous state is represented with the object, so instead of registering single callback, you get the object, which you can pass to many different functions, or on which you can listen for value with many different listeners. Promises also provide clean separation of success and error flows. It's much more powerful than plain callbacks, but also takes some time to get familiar with that. Once you get it, you will never go back ;-)
Superficially, this looks a lot like Python's Twisted framework with how they work with deferreds and callbacks. Are you familiar with it? Are the parallels appropriate, or is there something significantly different that I'm missing here? (I've been working with twisted for over 5 years now, so I'm _really_ comfortable with it.)
--
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
--
function mongoSearch(_, q) { var t0 = new Date(); var db = new mongodb.Db('tutorial', new mongodb.Server("127.0.0.1", 27017, {})); db.open(_); try { var coln = db.collection('movies', _); if (coln.count(_) === 0) coln.insert(MOVIES, _); var re = new RegExp(".*" + q + ".*"); return coln.find({ $or: [{ title: re }, { director: re }] }, _).toArray(_).map(function(movie) { return movie.title + ': ' + movie.director; }).join('<br/>') + '<br/>completed in ' + (new Date() - t0) + ' ms';; } finally { db.close(); } }It contains 6 asynchrounous calls to the mongodb API: db.open, db.collection, coln.count, coln.insert, coln.find and toArray mixed with standard javascript constructs: if, return, chaining (a(_).b()), even try/finally.
--
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
For more options, visit this group at
Some people clearly like promises, but the dominant pattern in node is not promises, it's callbacks, with complex callback issues managed with async.Stating your opinion strongly does not make it a fact. This is your preference, and many others, but not the majority.
If you write a library, it better use callbacks if you want people to use it.
One of them problems even with this approach is that you write and read code backwards. It executes bottom to top because you have to define named callback functions first. I find it much more natural with promises because not only is your code organized, but it reads top to bottom and is MUCH easier to trace flow.
--
In JavaScript the order you define things doesn't matter.
It does if your function declaration style is var funcName = function() { }; which is the style I use. Personal preference obviously.In JavaScript the order you define things doesn't matter.
--
Using callbacks in your own application code is the path of least resistance for using the majority of value in the node ecosystem.
I am new to Node and trying to decide between promises, asynch and vanilla, there are so many good arguments for each. Mikeal, do you mind expanding further how using promises in your own, non-shared, code could hinder use of node? Do a lot of the libs that depend on asynch require you to use asynch as well (or make things easier if you do)?
--
--
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/groups/opt_out.
That's overstating it a bit. It's very easy to convert to promise-based code, e.g.:
var fs = require('fs');
var Q = require('q');
var readFile = Q.denodeify(fs.readFile);
var writeFile = Q.denodeify(fs.writeFile);
readFile('src.txt').then(function (result) { writeFile('dest.txt', result); }).done();
Since this conversion process is pretty mechanical, there are several libraries that have done it for you, e.g. https://npmjs.org/package/pr