An interesting alternative is to use Q’s new “nodeify” function:

function returnsPromiseOrCallsback(callback) {
  return Q.resolve(5).nodeify(callback);
}

If callback is a function, it will call callback(null, 5); otherwise it will 
return the promise for 5. Similarly it will pass rejection reasons as the first 
parameter (i.e. as an error).

This allows you to create libraries that expose both promise-returning and 
callback-accepting APIs at the same time.

From: Andy
Sent: November 12, 2012 17:52
To: [email protected]
Subject: Re: [nodejs] Re: trying to wrap my head around "promises" - async vs Q


On Sunday, November 11, 2012 10:46:05 PM UTC-8, Mikeal Rogers wrote:

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.

I'm aware of the much larger async userbase on github. In my opinion this is 
because promises are a more complicated concept to learn and thus scare people 
off and reduce adoption. It's different than native javascript so there are 
people who do not want to explore the option.

If you write a library, it better use callbacks if you want people to use it.

I agree, as I said if you use expose an api you should still handle user 
responses with callbacks. but internally non promised code feels alien to me. I 
wouldn't give a user a promise as a response from my npm library.



--
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

-- 
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

Reply via email to