*Callbacks are silly. Callbacks are bad! Just play it
cool<https://github.com/kriskowal/q>,
there's fun to be had!
If your code is private, if it's no API, use promises my friend! You'll
soon see why!
Tired of error passing? Tired of nests? With deferred functions, you will
have no stress!*
var q = require('q');
q.ncall(db.open, db)
.then(function() {
// What we are doing here is WHEN the db.open statement is
finished
// we return a NEW promise saying that hey, we want to wait for
// db.collection to finish.
return q.ncall(db.collection, db, 'collection name')
}).then(function(collection) {
// Now we know our db.colleciton statement is done. What if
// there is no collection!?
// Example for what to do if you need conditional logic
if(!collection) {
return q.ncall(db.createCollection, db, 'colllection name');
}
}).then(function(collection) {
return q.ncall(db.query, db, collection, 'query');
}).then(function (queryResults) {
// I have query results! yay!!
console.log(queryResults);
}).fail(function(err) {
console.log('one of the above steps went irreversibly wrong!',
err);
});
*
But what, you ask, about external libs? Ones that need to use your code?
Won't with q they quib?
It's simple you see. For them you say:
I can use q all I want. But when I'm done, I'll send a callback your way!*
--
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en