There isn't really much difference in terms of implementation - in
each case you're creating an eventEmitter, doing some asynchronous
activity, and then emitting an event. The callback mechanism is just
syntatic sugar over a promise.
I would lean towards using a promise for those db calls, unless you'd
want to return the rows from a select as a multipart stream. In terms
of api design, you can use an optional parameter to allow for all
three patterns, with a slight modification:
function foo(x,opt) {
var bar = new process.Promise();
if (opt && opt.success) bar.addListener("success",opt.success);
someAsyncFunction(x, function() {
bar.emitSuccess();
});
return bar;
}
These three calls would then be equivalent:
foo(12,{ "success": function() { sys.puts("hello"); });
foo(12).addCallback(function() { sys.puts("hello"); });
foo(12).addListener("success", function() { sys.puts("hello"); });
and "hello" would be output once someAsyncFunction executed.
Cheers,
Andrew
> --
>
> 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.
>
--
Andrew Lunny
Software Developer, Nitobi
604 685 9287
andrew...@nitobi.com