On Tuesday, April 15, 2014 6:20:05 AM UTC+2, Andrew de Andrade wrote: > > So at work we're working on a bunch of node modules that will eventually > be published as open-source and I'm in favor of callbacks and two of my > co-workers are in favor of promises. We've discussed supporting both API > interfaces and I was curious what the general consensus of the community > was with respect to supporting both and the best way to name functions and > methods to support both. > > That being said, there are three obvious choices: > > (a) two function types: (1) synchronous functions; and (2) async functions > that return promises but also handle callbacks > (b) three function types: (1) synchronous functions; (2) async callback > functions; and (3) async promise functions > (c) two function types: (1) synchronous functions; (2) async callback > functions; > > I use promises with Bluebird and I prefer option (c), as long as you also expose all the prototypes of your library. Bluebird has an excellent automated wrapper called promisifyAll [1] that creates functions with the suffix Async which return promises, but it does require that your library uses prototypes instead of creating new objects every time.
This wrapper also allows you to distinguish operational errors from programmer errors (if you want to). Errors passed via callbacks are given an additional property `isAsync = true` and the promises returned by bluebird have an `.error(handler)` method that allows you to capture only errors tagged with this additional property. For now, this flag is just a bluebird internal convention and not yet "standardized" in promise libraries. In light of the errors article [2], perhaps we should rename the flag to `isOperational` and make it an official public convention of bluebird - in the hope that other promise libraries also adopt it. Why I think promises (together with arrow function syntax) are better than callbacks or generators isn't really relevant to this discussion, so I decided not to post it here but in a gist instead [3] [1]: https://github.com/petkaantonov/bluebird/blob/master/API.md#promisepromisifyallobject-target---object [2]: http://www.joyent.com/developers/node/design/errors [3]: https://gist.github.com/spion/33b3bf013cc9181171b9 -- -- 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.
