1.0.18 • Published 9 years ago
connect-promiser v1.0.18
Connect Promiser
Connect Promiser is very simple middleware that lets you use promises to write your route handlers (and other middleware) without having to worry about explicity error handling. It mainly serves to stop you having to wrap every async function in a try/catch
.
Usage
Setup
Connecting the middleware is easy, just app.use
the require( "connect-promiser" )
.
Using Promises
The middleware adds a function to res
under res.promise
. This function takes a next
function and either a promise
, or a () => promise
.
If the promise is rejected, it is caught, and the error is passed to the next
function as next(error)
Examples
Adding middleware
app.use( function( _req, res, next ) {
res.promise( next, async function() {
res.foobar = await doSomethingAsync();
next();
});
});
Sending a JSON response
app.use( function( req, res, next ) {
res.promise( next, async function() {
res.json( { result : await getSomethingAsync() } );
});
});