node-deferred-listener
Extends Node.js requestListeners with q's deferred promise support.
With this library you can turn any listener function that's
function(req, res[, next)
into promise capable and still keep it
working the same way.
You can use it with various frameworks and libraries:
License
It's open source, MIT.
Installation
You can install it simply from NPM:
npm install deferred-listener
Example how to turn existing Connect middlewares to support promises
var deferred_listener = ;var connect = ;var app = ;
Example how to build Connect middlewares with promises
deferred_listener()
can be used also when building your own new
middleware.
Let's build a simple middleware that fetches some data from the
filesystem and parses it to req.file
.
Look, no next()
!
{ key = key || file; var readFile = Q; return ;}
...and here's how to use it:
var app = ;
Reference
deferred_listener(requestListener)
The requestListener
is a function in format function(request, response[, next])
. It's common format for various different Node.js
middlewares.
The next
is an optional function in format function([err])
.
deferred_listener()
returns a wrapper function to requestListener
.
Returned function acts just like the original function but has
additional support for promises. It can still be called with the next
callback but instead of calling the original callback directly it will
only reject or resolve single promise and call the original next
only
once. However when called without a next
callback only a promise will
be returned.