On Apr 19, 2014, at 12:28 AM, Kevin Burton <[email protected]>
wrote:
> 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?
>
Kevin,
broadly, in the Q/promises world, it may help to think of functions as one of
four types: (1) something that returns a value immediately, (2) something that
is asynchronous and returns a value later to a callback using the node
convention callback(err, result), (3) something that is asynchronous and
returns a value later to a callback in a manner different from the node
convention (or via an event, which I'll ignore here), and finally, what we
want: (4) something that returns a promise (whose return value can be obtained
using its then() method).
To structure your code using promises, you may want some or all such functions
to return promises. Of course only the last one does, and so, Q provides a few
mechanisms to coerce the others types to do so as well:
denodify/ninvoke/nbind/etc are examples for type (2). Q.fcall() lets you wrap a
return value as a promise. And Q.defer() lets you create and return your own
promises for #4 or solve #3.
Say you want to connect to a DB using a node-db module that uses standard Node
conventions, and you want to insert a record, and finally write a log message
using an async log routine that does not follow node conventions. You might do
this (perhaps a bit contrived, to demonstrate the various Q-bits):
var Q = require('q');
var db = require('db');
var logger = require('logger');
Q.ninvoke(db, "connect", { host : 'localhost', user : 'notroot', password ;
'super secret' })
.then
(
function(dbconn)
{
return( Q.ninvoke(dbconn, 'insert', { first : 'Johnny',
last : 'Appleseed', device : '5s' } ));
}
)
.then(write_log)
.fail(function(err) { console.error(err); })
.done(process.exit);
function write_log(result)
{
var deferred = Q.defer(); // we are going to wrap the async
logger in a promise
var logmsg = "DB :: INSERT :: new ID " + result.id;
logger.write
(
logmsg,
function(rc)
{
if( rc === -1 ) // logging failed
deferred.reject(new Error("Write to DB log
failed."));
else
deferred.resolve();
}
);
// we return a promise which will be fulfilled when logger.write()
returns
return(deferred.promise);
}
Note that all function references passed to .then() are to promise-returning
functions. In write_log() you see an example of how you can create your own
promise and return it, in this case, wrapping an async routine that uses a
non-node style callback.
In the above, we handle errors that can occur at any step in the chain at the
end in the fail() method, but Q will let you catch them in the then() if you
provide a second function reference: promise.then(success_func, fail_func). In
either case, Q is smart enough to pass the first argument (err, in node style)
to fail_func (or the function referenced in fail()) and the second argument
(the result of the async operation in node style) to success_func.
If you have more questions specifically about Q promises, you may also wish to
join the Q mailing list (https://groups.google.com/forum/#!forum/q-continuum)
and ask there.
Regards,
--ravi
--
--
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
---
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 [email protected].
For more options, visit https://groups.google.com/d/optout.