-redis-db because this probably won't interest them as much
Promises have quite a few advantages over callbacks, and tend to fit the
client/server interaction model very nicely. The majority of the time when
you call an asynchronous method in node the return value is undefined (or
otherwise meaningless). All the interesting stuff happens in the callback.
This isn't true with promises. If an asynchronous method returns a promise
for the result you now have a first-class object that you can pass around
or aggregate with other values.
Another advantage of using promises is that you have a really useful way to
propagate errors through the "call stack", just like you normally would do
using `throw` in a sync function. When writing servers in particular this
property is absolutely invaluable.
For example, in lots of node documentation examples you'll see the
following line:
doSomethingAsync(args, function (err, result) {
if (err) throw err;
// carry on
});
This works fine when you're writing documentation (I guess) but if you put
`if (err) throw err;` into some server code then you've basically planted a
little time bomb that will eventually crash your server.
Instead, if that function returned a promise you could simply:
doSomethingAsync(args).then(function (result) {
// carry on
});
and whoever calls doSomethingAsync would be responsible for "catching" the
error. Basically promises add `return` and `try`/`catch` back to async
programming.
Anyway, this is getting long (sorry!). Hopefully I've given you a few
ideas. I've been meaning to write down my thoughts on async programming
with promises for a while now. This may be the thing that gets me started.
:)
--
Michael Jackson
@mjackson
On Fri, Mar 8, 2013 at 1:01 AM, alessioalex <[email protected]>wrote:
> Hey,
>
> Just out of curiosity: why do you prefer promises to callbacks?
>
> - Alex
>
>
> On Thursday, March 7, 2013 7:16:29 PM UTC+2, Michael Jackson wrote:
>
>> :D Thanks David.
>>
>> I've been programming almost exclusively in promises these past few
>> months and it feels pretty good. Plus, I'm a big believer in tiny modules
>> and simple code bases. Helps me grok them a bit easier when they're small.
>>
>> Also, thanks for your work on node_redis!
>>
>>
>> --
>> Michael Jackson
>> @mjackson
>>
>>
>> On Wed, Mar 6, 2013 at 8:54 PM, DTrejo <[email protected]> wrote:
>>
>>> Redis is 300 LOC!? ;)
>>>
>>> Cheers and good to have some competition to make us node_redis
>>> maintainers get into gear.
>>> D
>>>
>>> On Wednesday, March 6, 2013 1:23:25 PM UTC-5, Michael Jackson wrote:
>>>>
>>>> Hello,
>>>>
>>>> then-redis is a small, promise-based Redis client for node.js that I've
>>>> been working on over the past few weeks. It supports all the features of
>>>> Redis in a simple, user-friendly package. I thought others might enjoy
>>>> using it as well, so I released the source this morning.
>>>>
>>>> https://github.com/mjijackson/****then-redis<https://github.com/mjijackson/then-redis>
>>>>
>>>> The two major differences between then-redis and node_redis are:
>>>>
>>>> 1. then-redis returns a promise when you issue a command
>>>> 2. The entire codebase is very small (~300 LOC), just like Redis
>>>>
>>>> Other than that the APIs are very similar. If you're using Redis and
>>>> you enjoy promise-style programming please check it out and let me know
>>>> what you think.
>>>>
>>>> Install it: npm install then-redis
>>>>
>>>> Enjoy!
>>>>
>>>> --
>>>> Michael Jackson
>>>> @mjackson
>>>>
>>> --
>>> --
>>> Job Board: http://jobs.nodejs.org/
>>> Posting guidelines: https://github.com/joyent/**node/wiki/Mailing-List-*
>>> *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
>>> nodejs+un...@**googlegroups.com
>>>
>>> For more options, visit this group at
>>> http://groups.google.com/**group/nodejs?hl=en?hl=en<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<https://groups.google.com/groups/opt_out>
>>> .
>>>
>>>
>>>
>>
>> --
> --
> 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/groups/opt_out.
>
>
>
--
--
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/groups/opt_out.